asp.net core 3.1配置log4net
1.NuGet包:
Microsoft.Extensions.Logging
Microsoft.Extensions.Logging.Log4Net.AspNetCore
添加配置文件 log4net.config
<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="ErrorAppender" type="log4net.Appender.RollingFileAppender"> <!--日志文件路径--> <param name="File" value="Logs\\Error\\" /> <!--是否是向文件中追加日志--> <param name="AppendToFile" value="true" /> <!--log保留天数--> <param name="MaxSizeRollBackups" value="1000" /> <!--最大文件大小--> <param name="MaxFileSize" value="10240" /> <!--日志文件名是否是固定不变的--> <param name="StaticLogFileName" value="false" /> <!--日志文件名格式为:2008-08-31.log--> <param name="DatePattern" value="yyyy-MM-dd.'log'" /> <!--日志根据日期滚动--> <param name="RollingStyle" value="Date" /> <!--信息日志布局--> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%n==========%n【日志级别】:%-5level%n【记录时间】:%date %n【执行时间】:[%r]毫秒%n%message%n" /> </layout> <!--FATAL > ERROR > WARN > INFO > DEBUG--> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="ERROR" /> <param name="LevelMax" value="FATAL" /> </filter> </appender> <!-- 信息日志附加介质--> <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"> <!--日志文件路径--> <param name="File" value="Logs\\Info\\" /> <!--是否是向文件中追加日志--> <param name="AppendToFile" value="true" /> <!--log保留天数--> <param name="MaxSizeRollBackups" value="100" /> <param name="MaxFileSize" value="1" /> <!--日志文件名是否是固定不变的--> <param name="StaticLogFileName" value="false" /> <!--日志文件名格式为:2008-08-31.log--> <param name="DatePattern" value="yyyy-MM-dd.'log'" /> <!--日志根据日期滚动--> <param name="RollingStyle" value="Date" /> <!--信息日志布局--> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%n==========%n【日志级别】:%-5p%n【记录时间】:%d [%t]%n【信息详情】:%m%n" /> </layout> <!--FATAL > ERROR > WARN > INFO > DEBUG--> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="DEBUG" /> <param name="LevelMax" value="WARN" /> </filter> </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="InfoAppender" /> <appender-ref ref="TraceAppender" /> <appender-ref ref="ConsoleAppender" /> <appender-ref ref="ErrorAppender" /> </root> </log4net>
2.配置 CreateHostBuilder
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureLogging((context, loggingBuilder) => { loggingBuilder.AddFilter("System", LogLevel.Information); loggingBuilder.AddFilter("Microsoft", LogLevel.Information); var path = context.HostingEnvironment.ContentRootPath; loggingBuilder.AddLog4Net($"{path}/log4net.config");//配置文件 }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
3.使用
[Route("[controller]")] [ApiController] [AllowAnonymous] public class TestController : ControllerBase { private readonly ILogger<TestController> _logger; public TestController(ILogger<TestController> logger) { _logger = logger; } [HttpGet] public string Get() { _logger.LogInformation("LogInformation:" + DateTime.Now.ToString()); _logger.LogWarning("LogWarning:" + DateTime.Now.ToString()); _logger.LogError("LogError:" + DateTime.Now.ToString()); _logger.LogDebug("LogDebug:" + DateTime.Now.ToString()); return DateTime.Now.ToString(); } }