简体   繁体   English

在 ASP.NET Core 启动期间使用 Serilog 和 Sentry

[英]Using Serilog and Sentry during ASP.NET Core startup

I am trying to log errors thrown during app startup to sentry.我正在尝试将应用程序启动期间抛出的错误记录到哨兵。 I am using ASP.NET Core 2.1.2, with Serilog and Sentry.我正在使用 ASP.NET Core 2.1.2,以及 Serilog 和 Sentry。 If I use just sentry, it seems to work okay.如果我只使用哨兵,它似乎工作正常。

WebHost.CreateDefaultBuilder(args)
    .UseSentry("dsn")
    .UseStartup<Startup>()
    .Build()
    .Run();

However, if I add Serilog, the app does not send any events during startup, although it keeps working if an exception is thrown from a controller action.但是,如果我添加 Serilog,该应用程序在启动期间不会发送任何事件,尽管如果控制器操作引发异常,它仍会继续工作。

WebHost.CreateDefaultBuilder(args)
    .UseSerilog(
        (hostingContext, loggerConfiguration) =>
            loggerConfiguration
                .Enrich.FromLogContext()
                .MinimumLevel.Debug()
                .WriteTo.Console()
                .WriteTo.Sentry(s => {
                    s.MinimumBreadcrumbLevel = LogEventLevel.Debug;
                    s.MinimumEventLevel = LogEventLevel.Error;
                }))
    .UseSentry("dsn")
    .UseStartup<Startup>()
    .Build()
    .Run();

I'm trying to log from both the ConfigureServices and Configure methods in the Startup class:我正在尝试从 Startup 类中的ConfigureServicesConfigure方法登录:

public class Startup
{
    public Startup(IConfiguration configuration, ILogger<Startup> logger)
    {
        Configuration = configuration;
        Logger = logger;
    }

    public IConfiguration Configuration { get; }
    public ILogger<Startup> Logger { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        Logger.LogError(new Exception("something went wrong while configuring services"), "ERROR");

        services
            .AddMvc()
            .AddJsonOptions(options =>
            {
                options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        Logger.LogError(new Exception("something went wrong while configuring app"), "ERROR");
    }
}

I guess it has something to do with the order of the initialization of each logger, but I don't undertand the startup process enough to come up with a solution or at least an explanation.我想这与每个记录器的初始化顺序有关,但我对启动过程的了解不足以提出解决方案或至少是解释。 Any idea why this happens and how could I fix it?知道为什么会发生这种情况,我该如何解决?

Update更新

It seems that explicity adding the DNS to the serilog configuration also fixes the issue, although this seems to be against the samples in the sentry repo.似乎明确地将 DNS 添加到 serilog 配置也解决了这个问题,尽管这似乎与哨兵存储库中的样本背道而驰。

.UseSerilog(
        (hostingContext, loggerConfiguration) =>
            loggerConfiguration
                .Enrich.FromLogContext()
                .MinimumLevel.Debug()
                .WriteTo.Console()
                .WriteTo.Sentry(s => {
                    s.MinimumBreadcrumbLevel = LogEventLevel.Debug;
                    s.MinimumEventLevel = LogEventLevel.Error;
                    s.Dsn = new Dsn(dsn);
                }))

The ASP.NET Core logging infrastructure is built before the application request pipeline. ASP.NET Core 日志记录基础结构是在应用程序请求管道之前构建的。 For that reason, we can capture exceptions that happen during app start only via a Logging integration.因此,我们只能通过 Logging 集成捕获在应用程序启动期间发生的异常。

The reason that application initialization errors are captures when you use only Sentry.AspNetCore is that this package depends onSentry.Extensions.Logging .使用Sentry.AspNetCore时会捕获应用程序初始化错误的原因是此包依赖于Sentry.Extensions.Logging

When you do UseSentry , it internally hooks into the frameworks logging infrastructure.当您执行UseSentry ,它会在内部挂钩到框架日志记录基础结构中。 Crashes in the Startup class happen before the app is actually built (the completion of ConfigureServices and Configure are required to boot the app) so only using the Logging integration we're able to capture errors at that point. Startup类中的崩溃发生在实际构建应用程序之前(需要完成ConfigureServicesConfigure才能启动应用程序),因此仅使用 Logging 集成我们就能够捕获此时的错误。

Once you add Serilog to your app, the default logging backend gets replace by it .Serilog添加到您的应用程序后,默认日志记录后端将被 it 替换 That means that the hooks to the logging infrastructure done by Sentry.Extensions.Logging no longer take effect now that Serilog has taken over.这意味着Sentry.Extensions.Logging完成的对日志记录基础结构的挂钩不再生效,因为Serilog已经接管了。

For that reason you need to add the package Sentry.Serilog too.因此,您还需要添加Sentry.Serilog包。 In this case, with both packages added, you're able to capture bootstrapping errors only if you initialize the SDK via the Serilog integration (as you did per your last comment).在这种情况下,添加两个包后,只有通过Serilog集成初始化 SDK 时,您才能捕获引导错误(正如您根据上次评论所做的那样)。 That's not done in the samples to avoid confusion (why do I need to provide my DSN twice?) but what happens under the hood is that the first initialization, done by Serilog is only really useful until the Startup process is finished.在示例中没有这样做以避免混淆(为什么我需要两次提供我的 DSN?)但在幕后发生的是,由 Serilog 完成的第一次初始化只有在Startup过程完成后才真正有用。 Once Sentry.AspNetCore is initialized, it'll close the first client created by the Serilog integration and flush all events and add a new client to the main scope.一旦Sentry.AspNetCore被初始化,它将关闭由Serilog集成创建的第一个客户端并刷新所有事件并向主范围添加一个新客户端。 This makes sure things like request data are also added to the event.这确保诸如请求数据之类的内容也被添加到事件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM