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;

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top