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.

Was it helpful?

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;

Reference: https://github.com/serilog/serilog/commit/b289dbcde3e0f7366d90daf66e00c94f4cc58de3#diff-4934d624fc1d467b08411d520972e840R48

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top