Serilog将空白CorrelationId发送到Seq记录器

问题描述 投票:2回答:1

我正在.net核心Web API中使用Serilog和Seq来处理日志记录。我添加了对Steve Gordon的CorrelationId Middleware的引用,以从标头中获取X-Correlation-ID(或创建新的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); }
    }
}

任何想法为什么?

.net-core serilog seq-logging
1个回答
1
投票

[当我执行以下操作时,它对我有用(不作为中间件):

using (LogContext.PushProperty("CorrelationId", "This is a test..."))
{
    Log.Write(level, exception, messageTemplate, propertyValues);
}

因此,我怀疑这是您的CorrelationId被ASP.NET Core MVC覆盖,如下所示:https://github.com/serilog/serilog-aspnetcore/issues/59

3.0中该问题的一些背景及其解决方案也位于此处:https://github.com/aspnet/AspNetCore/issues/5918

抱歉,这不是一个“修复”,但是我遇到了同样的问题,并且能够通过在日志调用(在我的代码中)和对Log.Write的调用之间添加一个间接级别来解决该问题。如上所示)。

© www.soinside.com 2019 - 2024. All rights reserved.