MVC 6 中的 CorrelationID 在哪里

Where is the CorrelationID in MVC 6

如何使用 MVC 6 获取请求的 correlationID?

我想在记录消息时使用它,以便我可以通过系统跟踪请求。

在以前的版本中,我会使用 HttpRequestMessageExtensions.GetCorrelationId 方法: https://msdn.microsoft.com/en-us/library/system.net.http.httprequestmessageextensions.getcorrelationid%28v=vs.118%29.aspx

在最新版本中,HttpContext 直接公开了一个 TraceIdentifier 属性 可以用作关联标识符:https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.AspNetCore.Http.Abstractions/HttpContext.cs#L72

public void MvcAction() {
    var identifier = HttpContext.TraceIdentifier;
}

在旧版本中,您可能必须使用 IHttpRequestIdentifierFeature 功能来检索请求标识符:

public void MvcAction() {
    var feature = HttpContext.Features.Get<IHttpRequestIdentifierFeature>();
    var identifier = feature.TraceIdentifier;
}