Write to ULS log using PowerShell in SharePoint 2013

Write to ULS log using PowerShell in SharePoint 2013
InfoPath alternatives for form designing SharePoint

In this post we will discuss how we can write to sharepoint 2013 uls logs using PowerShell in SharePoint 2013. SharePoint maintains Logs and Exceptions of SharePoint in ULS (Unified Logging Service). By default SharePoint stored in .log files in 15 hive folder (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\LOGS). But you can change the default path to some other location, you can check sharepoint 2013 change uls log location.

In a real world scenario to get a log details from ULS logs is difficult without using a tool, You can use SharePoint LogViewer to get logs details in SharePoint 2013. In most of the times SharePoint throghs a Correlation ID instead of the real error details. By using PowerShell you can retrieve original error messages based on correlation id. You can read: Get Error Details based on Correlation ID from ULS Logs in SharePoint 2013 using PowerShell

We can also write our custom messages to ULS logs using PowerShell or SharePoint server object model.

Below is the PowerShell code to write to ULS logs. We can put the below code in Windows PowerShell ISE

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$diagSvc = [Microsoft.SharePoint.Administration.SPDiagnosticsService]::Local

$category = new-object Microsoft.SharePoint.Administration.SPDiagnosticsCategory(“My Custom Category”,

[Microsoft.SharePoint.Administration.TraceSeverity]::Monitorable,

[Microsoft.SharePoint.Administration.EventSeverity]::Error )

$diagSvc.WriteTrace(0, $category, [Microsoft.SharePoint.Administration.TraceSeverity]::Monitorable, “This is our custom error message writted from PowerShell” )

After this if you want to check in ULS logs file you can see it will write like below:

05/10/2016 12:22:14.58 PowerShell_ISE.exe (0x9598) 0x65C4 Unknown My Custom Category 00000 Monitorable This is our custom error message writted from PowerShell cf54b83a-a9d2-0006-4a21-56cfd2a9d101

Write to ULS log using PowerShell in SharePoint 2013

Write to ULS log using PowerShell in SharePoint 2013Write to ULS logs using SharePoint server object model:
You can also use SharePoint 2013 server side object model code to write to uls log. You can use the below code in a visual web part.

SPDiagnosticsCategory category = new SPDiagnosticsCategory(“My Custom Category”,TraceSeverity.High,EventSeverity.ErrorCritical);

SPDiagnosticsService.Local.WriteTrace(0,category,TraceSeverity.High,”This is our custom error message writted from SharePoint 2013 Server object model”);

Hope this will be helpful.

Similar SharePoint 2013 Tutorials

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →

Leave a Reply