How to Access the Correlation ID in Code

As you found this post you probably know that a correlation ID uniquely identifies each request SharePoint 2010 handles. And that it is used across WFEs and even in SQL Server–makes making sense of all those logs sooo much easier!

But how do you get a request’s correlation ID in code to show it in your own error messages? It’s just one unmanaged call but it took me ages to find this. All credits go to Wictor Wilén for coming up with the solution. I repost it here mostly for my own reference.

// Import native function
[DllImport("advapi32.dll")]
public static extern uint EventActivityIdControl(uint controlCode, ref Guid activityId);
public const uint EVENT_ACTIVITY_CTRL_GET_ID = 1;
// Read correlationID 
Guid correlationID = Guid.Empty;
EventActivityIdControl(EVENT_ACTIVITY_CTRL_GET_ID, ref correlationID );
// Use correlationID ...

External References

  • Wictor Wilén’s blog post
  • Thorough explanation of correlation IDs in this blog post
  • Maybe you can win a prize by solving the riddle in this msdn topic?
  • Advertisements
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

One Response to How to Access the Correlation ID in Code

  1. Pingback: Using SharePoint 2010′s Logging Infrastructure, Part 1 | 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