How to access the ASP.NET / IIS ContextId
题
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.
解决方案
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
其他提示
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;