Friday, 10 December 2010

Write to the SharePoint ULS log files from your code

Writing to SharePoint's log files has been made very easy in SharePoint 2010.

All you have to du is call SPDiagnosticsService.Local.WriteTrace (located in Microsoft.SharePoint.Administration). First an example:

Microsoft.SharePoint.Administration.SPDiagnosticsService.Local.WriteTrace(
  0,
  new Microsoft.SharePoint.Administration.SPDiagnosticsCategory("ListTimerJob",
                            Microsoft.SharePoint.Administration.TraceSeverity.Medium,
                            Microsoft.SharePoint.Administration.EventSeverity.Information
                            ),
  Microsoft.SharePoint.Administration.TraceSeverity.Medium,
  "The list {0} found",
  new object[] { "ListTimerJob" }
);


Ordered from the most important/critical to the least the TraceSeverity levels can be set to: Unexpected, Monitorable, High, Medium, Verbose, None.
In the same order the EventSeverity levels is: Critical, Error, Warning, Information, Verbose, None.

A description of the levels can be found here: http://technet.microsoft.com/en-us/library/ee748656.aspx

My own log messages shows up in the SharePoint log file now.



I passed "ListTimerJob" as the first parameter to the SPDiagnosticsCategory above - this shows up in the Category coloumn, which makes it easy to make I filter un only my messages.


See also (links to msdn)
Using the Trace Logging API
SPDiagnosticsServiceBase Class

No comments:

Post a Comment