在Sentry.serilog中添加自定义动态标签

发布于 2025-01-28 08:55:29 字数 1710 浏览 4 评论 0 原文

我正在尝试通过corseLationId属性来丰富日志,我有这样的中间件,在这里,我在其中生成并将属性推向serilog logcontext

public class LogContextMiddleware : IMiddleware
{
    private readonly ICorrelationIdFactory _correlationIdFactory;

    public LogContextMiddleware(ICorrelationIdFactory correlationIdFactory)
    {
        _correlationIdFactory = correlationIdFactory;
    }

    public async Task InvokeAsync(HttpContext context, RequestDelegate next)
    {
        var correlationId = _correlationIdFactory.Create();
        using(LogContext.PushProperty("CorrelationId", correlationId))
        {
            await next(context);
        }
    }
}

具有Sentry.serilog设置为这样:

 .WriteTo.Sentry(o =>
                                      {
                                          o.MinimumBreadcrumbLevel = LogEventLevel.Information; 
                                          o.MinimumEventLevel = LogEventLevel.Error; 
                                          o.Dsn = "<dsn url>";
                                          o.AttachStacktrace = true;
                                          o.TextFormatter = new MessageTemplateTextFormatter("[{CorrelationId}] {Message}", null);
                                      });

但是我在任何标签中都没有看到CorralationId,在sentry UI中的标签部分中,或在消息格式化的文本输出中。只是在这里空[]。

信息

[]无法创建“ “ 因为'“令牌为空”'

我错过了配置的什么?提前致谢!

I am trying to enrich logs with CorrelationId property, I have such middleware where, I am generating and pushing property to Serilog LogContext

public class LogContextMiddleware : IMiddleware
{
    private readonly ICorrelationIdFactory _correlationIdFactory;

    public LogContextMiddleware(ICorrelationIdFactory correlationIdFactory)
    {
        _correlationIdFactory = correlationIdFactory;
    }

    public async Task InvokeAsync(HttpContext context, RequestDelegate next)
    {
        var correlationId = _correlationIdFactory.Create();
        using(LogContext.PushProperty("CorrelationId", correlationId))
        {
            await next(context);
        }
    }
}

Having Sentry.Serilog setup like that:

 .WriteTo.Sentry(o =>
                                      {
                                          o.MinimumBreadcrumbLevel = LogEventLevel.Information; 
                                          o.MinimumEventLevel = LogEventLevel.Error; 
                                          o.Dsn = "<dsn url>";
                                          o.AttachStacktrace = true;
                                          o.TextFormatter = new MessageTemplateTextFormatter("[{CorrelationId}] {Message}", null);
                                      });

But I am not seeing CorrelationId in any tags neither in tags section in Sentry UI nor in message formatted text output. Just empty [] here.

MESSAGE

[] Failed to create identity user for "[email protected]"
because '"Token is empty"'

What did I miss in configuration? Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初相遇 2025-02-04 08:55:29

我不确定它是否是正确的解决方案,但也许会对您有所帮助。

    app.Use((context, next) =>
    {

     var hub = context.RequestServices.GetRequiredService<IHub>();
     var _correlationIdFactory = context.RequestServices.GetRequiredService<ICorrelationIdFactory>();
     hub.ConfigureScope(s =>
     {
         var correlationId = _correlationIdFactory.Create();
         s.SetTag("CorrelationId",correlationId);
     });

     return next();
    });

I am not sure if it is correct solution, but maybe it will help for you.

https://github.com/getsentry/sentry-dotnet/blob/main/samples/Sentry.Samples.AspNetCore.Serilog/Program.cs#L53

    app.Use((context, next) =>
    {

     var hub = context.RequestServices.GetRequiredService<IHub>();
     var _correlationIdFactory = context.RequestServices.GetRequiredService<ICorrelationIdFactory>();
     hub.ConfigureScope(s =>
     {
         var correlationId = _correlationIdFactory.Create();
         s.SetTag("CorrelationId",correlationId);
     });

     return next();
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文