我在 .net 核心 web api 中使用 Serilog 和 Seq 来处理日志记录。我添加了对 Steve Gordon 的CorrelationId 中间件的引用,以从标题中获取 X-Correlation-ID(或创建新的)以更新 TraceIdentifier。
然后,我添加了另一个中间件类以将相关 ID 推送到日志上下文中,但这无法正常工作:
public class LogCorrelationIdMiddleware
{
private readonly RequestDelegate _next;
public LogCorrelationIdMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext context)
{
// doesn't work - blank CorrelationId in Seq
using (LogContext.PushProperty("CorrelationId", context.Request.HttpContext.TraceIdentifier)) { return _next(context); }
// works - correct CorrelationId2 in Seq
//using (LogContext.PushProperty("CorrelationId2", context.Request.HttpContext.TraceIdentifier)) { return _next(context); }
}
}
任何想法为什么?