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:

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.

Diagnostic Logging Configuration

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).

And calling 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.