How can I get current request id?
wziska opened this issue · comments
Hi,
Although it's very helpful to have request id included in logs, I would also like to return this Id to the user on error screen. I know I can get it directly from current HttpContext, but it might not be there yet and I have to know internals of HttpRequestIdEnricher.
Thanks
Hi @wziska,
Instead of HttpRequestIdEnricher
, you can use HttpRequestTraceIdEnricher
, which uses an id pre-allocated by ASP.NET (may even be IIS, actually):
var serviceProvider = (IServiceProvider)HttpContext.Current;
var workerRequest = (HttpWorkerRequest)serviceProvider.GetService(typeof(HttpWorkerRequest));
var requestId = workerRequest.RequestTraceIdentifier;
HTH!
Unfortunately I'm getting Guid.Empty from RequestTraceIdentifier. Tried to add <httpTracing>
element to web.config, but it didn't help.
Thanks for the investigation. Build 1.1 of the package (CI should publish soon) now has HttpRequestIdEnricher.TryGetCurrentHttpRequestId()
. Cheers!
Thanks for quick feedback. I initially though about extension method for HttpContextExtensions
, but static method in HttpRequestIdEnricher
works too.
Regards.