1、添加NuGet包:Microsoft.Extensions.Logging.Log4Net.AspNetCore
2、增加配置Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging((context,ILoggingBuilder)=> {
ILoggingBuilder.AddFilter("System", LogLevel.Warning);
ILoggingBuilder.AddFilter("Microsoft", LogLevel.Warning);
ILoggingBuilder.AddLog4Net();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
3、添加log4net.config配置文件
<?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\log.txt" /> <!--是否是向文件中追加日志--> <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>
|
请发表评论