如何从Startup.cs中写入日志? [英] How do I write logs from within 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
类中.请注意,将来可能会删除网络托管服务商.因此,您无需登录 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屋!