欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ConsoleLoggerExtensions.AddConsole(ILoggerFactory)已过时代码修复

程序员文章站 2022-06-30 08:36:40
0x00.问题 netcoreapp2.2环境下, Startup.cs 代码配置如下 运行输出警告:“ConsoleLoggerExtensions.AddConsole(ILoggerFactory)”已过时:“This method is obsolete and will be remove ......

0x00.问题

netcoreapp2.2环境下, startup.cs 代码配置如下
1  public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory)
2   {
3       loggerfactory.addconsole();
4       
5        //……
6   }

 

运行输出警告:“consoleloggerextensions.addconsole(iloggerfactory)”已过时:“this method is obsolete and will be removed in a future version. the recommended alternative is addconsole(this iloggingbuilder builder).”

官方文档也注明此api已经过时。 https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.extensions.logging.consoleloggerextensions.addconsole?view=aspnetcore-2.2#microsoft_extensions_logging_consoleloggerextensions_addconsole_microsoft_extensions_logging_iloggingbuilder_

 

0x01.推荐方法

program.cs  配置如下

 1 public class program
 2 {
 3     public static void main(string[] args)
 4     {
 5         createwebhostbuilder(args).build().run();
 6     }
 7 
 8     public static iwebhostbuilder createwebhostbuilder(string[] args) =>
 9         webhost.createdefaultbuilder(args)
10             .usestartup<startup>() 
11             .configurelogging((hostingcontext, logging) =>
12             {
13                 logging.addconfiguration(hostingcontext.configuration.getsection("logging"));
14                 logging.addconsole();
15                 logging.adddebug();
16             });
17 }

 

或者 startup.cs  configureservices方法中配置如下

 1 public void configureservices(iservicecollection services)
 2 {
 3     services.addlogging(logging =>
 4     {
 5         logging.addconsole();
 6         logging.adddebug();
 7     }); 
 8 
 9     //……
10 }