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

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

问题描述

为了调试启动失败的 .NET Core 应用程序,我想从 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 the 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 可以将记录器注入到 Startup 类中.请注意,该 Web 主机可能会在未来某个时候被删除.因此,您应该尝试找到适合您的解决方案,而无需在 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天全站免登陆