How to access the ASP.NET / IIS ContextId
Question
I've seen way too many times in my event logs this "ContextId" for a given ASP.NET request. I've recently started looking into ETW events that are pushed out by ASP.NET, and want to re-use this ContextID in my own events that I fire.
How can I do this?
System.Threading.Thread.CurrentContext doesn't seem to have it. It's always 0.
Solution
This is a poorly documented feature, but yes you can get it.
It sits on the HttpWorkerRequest RequestTraceIdentifier property.
http://msdn.microsoft.com/en-us/library/system.web.httpworkerrequest.requesttraceidentifier.aspx
OTHER TIPS
You can get it from the HttpWorkerRequest.RequestTraceIdentifier
property. Note that obviously you need to have the IIS ETW (Event Tracing for Windows) feature enabled or this property will be Guid.Empty
. The following is how to get it from HttpContext
:
var serviceProvider = (IServiceProvider)HttpContext.Current;
var workerReqest = (HttpWorkerRequest)serviceProvider.GetService(typeof(HttpWorkerRequest));
var requestId = workerReqest.RequestTraceIdentifier;