如何修复过时的ILoggerFactory方法? [英] How to fix obsolete ILoggerFactory methods?
问题描述
我将项目升级到.NET Core 2.2.x,并收到了以下代码的过时警告-两行:
I upgraded my project to .NET Core 2.2.x and got an obsolete warning regarding the following code - both lines:
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
修复建议为建议的替代方法为AddConsole(此ILoggingBuilder构建器)
。我以为那是我在用的东西。
The suggestion to fix is The recommended alternative is AddConsole(this ILoggingBuilder builder)
. I thought that is what I am using.
我在这里缺少什么?
推荐答案
我今天遇到了同样的问题。
I had the same issue today.
从Startup.cs中删除您的日志记录配置,然后转到Program.cs文件并添加以下内容:
Remove your logging configuration from Startup.cs and go to your Program.cs file and add something like:
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
})
.Build();
此命令使用了 builder,因为变量 logging是一个IloggingBuilder(而您的代码仍然是使用ILoggerFactory)
This used the 'builder' because the variable 'logging' is an IloggingBuilder (whereas your code is still using ILoggerFactory)
更新:我刚刚尝试过的另一种方法是保留在Startup.cs中,但将日志记录内容从 Configure方法移动到 ConfigureServices :
UPDATE: The other method I just tried is to stay inside Startup.cs but move the logging stuff from the 'Configure' method to 'ConfigureServices' like this:
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddConfiguration(Configuration.GetSection("Logging"));
loggingBuilder.AddConsole();
loggingBuilder.AddDebug();
});
}
也许可以使Program.cs的污染更少...
Perhaps keeps the Program.cs less polluted...
这篇关于如何修复过时的ILoggerFactory方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!