Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, November 28, 2011

Writing errors to ULS logs and event logs Sharepoint 2010

Writing errors to ULS logs and event logs Sharepoint 2010

In SharePoint Foundation, ULS exposes configurable settings in two ways, through the:
* Object model
* Windows PowerShell cmdlets


Using Object Model - Developers can either write to Sharepoint ULS logs or to event Logs using SharePoint 2010 object model.
For writing to SharePoint ULS logs - We can use Diagnostics Service, which will make the customized categories viewable in the administrative UI for our errors.


example -

using Microsoft.SharePoint.Administration;
SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
diagSvc.WriteTrace(0, new SPDiagnosticsCategory(“Add category”, TraceSeverity.Monitorable,EventSeverity.Error),TraceSeverity.Monitorable,”Writing to the ULS log: {0}”,new object[] { “Error in WebPart!”});


For Writing to Event Log :

using Microsoft.SharePoint.Administration;

WriteEvent(0, new SPDiagnosticsCategory(“Add Category”, TraceSeverity.Monitorable, EventSeverity.Error),EventSeverity.Monitorable,”Writing to the Event log: {0}”,new object[] { “Error in WebPart!”});


Using Powershell - Microsoft SharePoint Foundation 2010 also provides the ability to use Windows PowerShell cmdlets for manipulating Unified Logging Service (ULS) log settings.

There are number of cmdlets available. I have listed some of the them below :

Get-SPDiagnosticConfig – Retrieves Diagnostic Configuration values.

Set-SPDiagnosticConfig – Allows setting Diagnostic Configuration values.

Get-SPLogLevel – Returns IDiagnosticsLevel2 objects or displays a list of diagnostics levels.

Set-SPLogLevel – Allows the user to set the trace and event level for a set of categories.

Clear-SPLogLevel – Resets the trace and event levels back to their default values.

New-SPLogFile – Ends the current log file and starts a new one.

Get-SPLogEvent – Reads/queries ULS trace logs.

Merge-SPLogFile – Combines trace log files from all farm servers into a single file

No comments:

Post a Comment