本文主要介绍.NET Core ILoggerFactory过时(obsolete)问题,项目升级到.NET Core 2.2.x并得到了代码的过时警告, 提示loggerFactory.AddConsole(Configuration.GetSection(“Logging”));代码过时,修复建议是The recommended alternative is AddConsole(this ILoggingBuilder builder)。

1、第一种解决方法

删除Startup.cs中日志记录配置,在Program.cs文件并添加如下代码:

  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();

2、第二种解决方法

保留在Startup.cs中,但将日志记录代码从"Configure"方法移动到"ConfigureServices",如下所示:

  public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddConfiguration(Configuration.GetSection("Logging"));
loggingBuilder.AddConsole();
loggingBuilder.AddDebug();
});
}

推荐文档