Update 2011-02-03: Added link to Corrrelation ID info.
Logging has become much easier in SharePoint 2010: write to SharePoint’s log using
SPDiagnosticsService.Local.WriteTrace() and write to Windows event logs using
SPDiagnosticsService.Local.WriteEvent(). But thankfully Microsoft thought of developers like you and me and kept things interesting …
I’ll use this first part of the post to talk about what goes where because I found the terminology pretty confusing at first. If you already know those basics you may want to go right to the implementation:
The Basics of SharePoint 2010’s Logging Infrastructure
Here’s yet another picture saving 1000 innocent words. It shows SharePoint 2010’s Diagnostic Logging configuration window.
- Each item under “All Categories” is an Area.
- Each item under an area is a Category.
- For each category you can specify how critical the message’s Trace Level has to be so the message actually makes it to SharePoint’s log.
- For each category you can also set an Event Level specifying which messages make it to Windows event logs–more on that in part 3.
The next screenshot shows SharePoint’s log. It’s in the LOGS directory of the 14-hive and unlike the Windows event logs it’s a simple text format. You’ll want to use a program like ULS Viewer (see External References section) to filter the messages, have new ones show up automatically, and important ones marked automatically.
The only things you’ll recognize from the Diagnostic Logging configuration window are the Category and (Trace) Level fields. To the Product field goes what was called Area there.
The Correlation ID uniquely identifies each web request so you can follow it throughout all the
parts categories of your software. See this post to find out how to access it programmatically.
But when you call
SPDiagnosticsService.Local.WriteTrace() you’ll find the Product field’s value is always “Unknown” like in the second screenshot. This is the first problem part 2 will solve. The second one will be to inform SharePoint about the areas, categories, and levels you are going to use so they show up in the Diagnostic Logging configuration window (first screenshot).
SPDiagnosticsService.Local.WriteEvent() will result in this error message:
SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.
Part 3 of this post will show how to get around this problem.
- The ULS viewer I used can be downloaded for free here.
- A blog post showing how to use this ULS viewer is here.
- Find Microsoft’s documentation of the design of the SharePoint Logger here.
- Find information about the pervasive Correlation ID here.
- Have a different view of Windows here