Reputation: 657
How do I get the correlationID of a request using MVC 6?
I want to use it when I log a message so that I can track a request through the system.
In previous versions I would have used the HttpRequestMessageExtensions.GetCorrelationId method: https://msdn.microsoft.com/en-us/library/system.net.http.httprequestmessageextensions.getcorrelationid%28v=vs.118%29.aspx
Upvotes: 6
Views: 4240
Reputation: 42010
In the newest versions, HttpContext
directly exposes a TraceIdentifier
property you can use as a correlation identifier: https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.AspNetCore.Http.Abstractions/HttpContext.cs#L72
public void MvcAction() {
var identifier = HttpContext.TraceIdentifier;
}
In older versions, you might have to use the IHttpRequestIdentifierFeature
feature to retrieve the request identifier:
public void MvcAction() {
var feature = HttpContext.Features.Get<IHttpRequestIdentifierFeature>();
var identifier = feature.TraceIdentifier;
}
Upvotes: 5