如何从Startup.cs中写入日志

编程入门 行业动态 更新时间:2024-10-25 21:30:02
本文介绍了如何从Startup.cs中写入日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

In order to debug a 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中写入日志

本文发布于:2023-11-14 14:02:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587410.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日志   Startup   cs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!