在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
第一步:nuget 引用Microsoft.Extensions.Logging.Log4Net.AspNetcore 第二步:Program指向log4Net 如果p.addLog4Net() 1 public static IHostBuilder CreateHostBuilder(string[] args) => 2 Host.CreateDefaultBuilder(args) 3 .ConfigureLogging(p => 4 { 5 var path = System.IO.Directory.GetCurrentDirectory(); 6 p.AddLog4Net($"{path}/config/log4net.config");//配置文件 7 }) 8 .ConfigureWebHostDefaults(webBuilder => 9 { 10 webBuilder.UseStartup<Startup>(); 11 }); 第三步:引入log4net配置文件 <log4net> <appender name="Console" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <!-- Pattern to output the caller's file name and line number --> <conversionPattern value="%date %5level %logger.%method [%line] - %property{scope} %property{test} MESSAGE: %message%newline %exception" /> </layout> </appender> <appender name="RollingFile" type="log4net.Appender.RollingFileAppender"> <file value="log/log.log" /> <appendToFile value="true" /> <maximumFileSize value="100KB" /> <maxSizeRollBackups value="2" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %5level %logger.%method [%line] - %property{scope} %property{test} MESSAGE: %message%newline %exception" /> </layout> </appender> <appender name="TraceAppender" type="log4net.Appender.TraceAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %5level %logger.%method [%line] - %property{scope} %property{test} MESSAGE: %message%newline %exception" /> </layout> </appender> <appender name="ConsoleAppender" type="log4net.Appender.ManagedColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="Red" /> </mapping> <mapping> <level value="WARN" /> <foreColor value="Yellow" /> </mapping> <mapping> <level value="INFO" /> <foreColor value="White" /> </mapping> <mapping> <level value="DEBUG" /> <foreColor value="Green" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %5level %logger.%method [%line] - %property{scope} %property{test} MESSAGE: %message%newline %exception" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="RollingFile" /> <appender-ref ref="TraceAppender" /> <appender-ref ref="ConsoleAppender" /> </root> </log4net>
|
请发表评论