Log4Net.AspNetCore 配置
1.引用 Microsoft.Extensions.Logging.Log4Net.AspNetCore
2.增加配置Program.cs
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureLogging((hostingContext, logging) => { logging.AddFilter("System", LogLevel.Warning); logging.AddFilter("Microsoft", LogLevel.Warning); logging.AddLog4Net(); }) .UseStartup<Startup>(); }
3.增加配置Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddLog4Net(); }
4.增加配置文件
<?xml version="1.0" encoding="utf-8" ?> <log4net> <appender name="DebugAppender" type="log4net.Appender.DebugAppender" > <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <!--指定日记记录方式,以滚动文件的方式(文件记录)--> <appender name="RollingFile" type="log4net.Appender.RollingFileAppender"> <!--日志路径--> <file value="Log/" /> <!--是否是向文件中追加日志--> <appendToFile value="true" /> <!--log保留天数--> <param name= "MaxSizeRollBackups" value= "10"/> <!--每个文件最大1M--> <param name="maximumFileSize" value="1MB" /> <!--日志根据日期滚动--> <param name="RollingStyle" value="Date" /> <!--日志文件名格式为:logs_20080831.log--> <param name="DatePattern" value=""logs_"yyyyMMdd".log"" /> <!--日志文件名是否是固定不变的--> <param name="StaticLogFileName" value="false" /> <!--布局--> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" /> </layout> </appender> <root> <level value="ALL"/> <appender-ref ref="DebugAppender" /> <appender-ref ref="RollingFile" /> </root> </log4net>