简体   繁体   English

ASP.NET Core 启动消息日志

[英]ASP.NET Core startup message log

I have an ASP.NET Core 3.1 app, which was upgraded from ASP.NET Core 2.0.我有一个 ASP.NET Core 3.1 应用程序,它是从 ASP.NET Core 2.0 升级的。 We would like to get the old startup messages ("Now listening on: http://localhost:5000", etc) to log through the configured structured logging options.我们希望通过配置的结构化日志选项获取旧的启动消息(“现在正在监听:http://localhost:5000”等)。 This works out of the box with new 3.1 projects, but I cannot figure out how to enable it in an existing project that does not use the ConfigureWebHostDefaults() method at startup.这适用于新的 3.1 项目,但我无法弄清楚如何在启动时不使用ConfigureWebHostDefaults()方法的现有项目中启用它。 For example, this logs startup through structured logging:例如,此日志通过结构化日志记录启动:

public static void Main(string[] args)
{
    CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

But this doesn't:但这不会:

public static void Main(string[] args)
{
    new WebHostBuilder()
        .UseKestrel()
        .ConfigureLogging((hostingContext, logging) =>
        {
            logging.AddDebug();
            logging.AddConsole();
        })
        .UseStartup<Startup>()
        .Build()
        .Run();
}

I found this question:我发现了这个问题:

What is the difference between ConfigureWebHostDefaults and ConfigureWebHost methods? ConfigureWebHostDefaults 和 ConfigureWebHost 方法之间有什么区别?

and scanned through the code linked there, but I'm coming up blank.并扫描了那里链接的代码,但我发现空白。 What's the magic incantation to connect Kestrel to the logging infrastructure?将 Kestrel 连接到日志记录基础设施的魔法咒语是什么?

In the end, we just needed to fully switch over to the new host builder ( CreateHostBuilder vs the older CreateWebHostBuilder ).最后,我们只需要完全切换到新的主机构建器( CreateHostBuilder与旧的CreateWebHostBuilder )。 We had tried it before, but when we added our custom logging provider in ConfigureLogging we ended up with duplicated log messages, so we rolled back to the old way without fully understanding the difference.我们之前尝试过,但是当我们在ConfigureLogging添加我们的自定义日志提供程序时,我们最终得到了重复的日志消息,因此我们在没有完全理解差异的情况下回滚到旧方式。 The missing bit was calling ClearProviders() .缺少的一点是调用ClearProviders() Before, we had:之前,我们有:

public static void Main(string[] args)
{
    CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args)
    .UseKestrel()
    .ConfigureAppConfiguration((context, config) => { ... }))
    .ConfigureLogging((hostingContext, logging) =>
    {
        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));

        logging.AddProvider(new CustomLoggerProvider());
        logging.AddDebug();
    })
    .UseStartup<Startup>();

And now:现在:

public static void Main(string[] args)
{
     CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
    .ConfigureAppConfiguration((hostingContext, config) => { ... } ))
    .ConfigureLogging((hostBuilder, logging) =>
    {                
        logging.ClearProviders();

        logging.AddConfiguration(hostBuilder.Configuration.GetSection("Logging"));

        logging.AddProvider(new CustomLoggerProvider());
        logging.AddDebug();
    })
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.UseStartup<Startup>();
    });

Now Kestrel nicely logs it's status messages to the preferred logger along with everything else.现在 Kestrel 很好地将它的状态消息与其他所有内容一起记录到首选记录器。

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

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