如何从Startup.cs中写入日志 [英] How do I write logs from within Startup.cs

查看:504
本文介绍了如何从Startup.cs中写入日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了调试启动失败的.net核心应用程序,我想从startup.cs文件中写入日志.我在该文件中有日志记录设置,该文件可在startup.cs文件之外的应用程序的其余部分中使用,但是不确定如何从startup.cs文件本身中写入日志.

In order to debug a .net core app which is failing on startup, I would like to write logs from within the startup.cs file. I have logging setup within the file that can be used in rest of the app outside the startup.cs file, but not sure how to write logs from within the startup.cs file itself.

推荐答案

.Net Core 3.1

不幸的是,对于ASP.NET Core 3.0,情况再次有所不同.默认模板使用HostBuilder(而不是WebHostBuilder)来设置新的通用主机,该主机可以承载多个不同的应用程序,而不仅限于Web应用程序.此新主机的一部分还包括删除先前为Web主机存在的第二个依赖项注入容器.最终,这意味着您将无法将IConfiguration之外的任何依赖项注入到Startup类中.因此,您将无法在ConfigureServices方法期间登录.但是,您可以将记录器注入到Configure方法中并在那里登录:

Unfortunately, for ASP.NET Core 3.0, the situation is again a bit different. The default templates use the HostBuilder (instead of the WebHostBuilder) which sets up a new generic host that can host several different applications, not limited to web applications. Part of this new host is also the removal of the second dependency injection container that previously existed for the web host. This ultimately means that you won’t be able to inject any dependencies apart from the IConfiguration into the Startup class. So you won’t be able to log during the ConfigureServices method. You can, however, inject the logger into the Configure method and log there:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger<Startup> logger)
{
    logger.LogInformation("Configure called");

    // …
}

如果您绝对需要 来登录ConfigureServices,则可以继续使用WebHostBuilder,这将创建旧版WebHost,该旧版WebHost可以将记录器注入到Startup类中.请注意,将来可能会删除网络托管服务商.因此,您无需登录ConfigureServices就可以尝试找到适合您的解决方案.

If you absolutely need to log within ConfigureServices, then you can continue to use the WebHostBuilder which will create the legacy WebHost that can inject the logger into the Startup class. Note that it’s likely that the web host will be removed at some point in the future. So you should try to find a solution that works for you without having to log within ConfigureServices.

.NET Core 2.x

随着ASP.NET Core 2.0的发布,这已经发生了重大变化.在ASP.NET Core 2.x中,日志记录是在主机生成器上创建的.这意味着默认情况下可以通过DI使用日志记录,并且可以将其插入Startup类:

This has changed significantly with the release of ASP.NET Core 2.0. In ASP.NET Core 2.x, logging is created at the host builder. This means that logging is available through DI by default and can be injected into the Startup class:

public class Startup
{
    private readonly ILogger<Startup> _logger;

    public IConfiguration Configuration { get; }

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

    public void ConfigureServices(IServiceCollection services)
    {
        _logger.LogInformation("ConfigureServices called");

        // …
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        _logger.LogInformation("Configure called");

        // …
    }
}

这篇关于如何从Startup.cs中写入日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆