Using SharePoint 2010’s Logging Infrastructure, Part 1

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.
Diagnostic Logging Configuration

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.

ULS log with Product field set to "Unknown"

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.

Part 3 of this post will show how to get around this problem.

External References

  • 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
Advertisements
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

2 Responses to Using SharePoint 2010’s Logging Infrastructure, Part 1

  1. Pingback: Using SharePoint 2010′s Logging Infrastructure, Part 2 | Dieter Bremes's Blog

  2. Pingback: Using SharePoint 2010′s Logging Infrastructure, Part 3 | Dieter Bremes's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s