微软日志工厂 Microsoft.Extensions.Logging 中增加 log4net 的日志输出
前提:
需要nuget Microsoft.Extensions.Logging.Log4Net.AspNetCore 2.2.6;
描述:解决 .net core 微软日志工厂 Microsoft.Extensions.Logging 增加 log4net 的日志输出,暂时分为 info,Error,Debug 三种类型,按天记录;
实际效果:
.net core webapi 解决办法:
办法1 修改 Program.cs 配置 log4net
public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .ConfigureLogging((context, logger) => { logger.AddFilter("System", LogLevel.Warning); logger.AddFilter("Microsoft", LogLevel.Warning); logger.AddLog4Net(); }); }
办法2 修改 startup.cs http管道配置 Configure
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddLog4Net(); .... }
注:以上两种方法只用一种就好,否则日志输出的时候会有多条 log4net 的记录。
控制器 调用 demo (使用构造函数注入):
public class HomeController : ControllerBase { /// <summary> /// 日志信息 /// </summary> ILogger<HomeController> logger; /// <summary> /// 构造函数配置日志 /// </summary> /// <param name="logger"></param> public HomeController(ILogger<HomeController> logger) { this.logger = logger; this.logger.LogError($"{DateTime.Now} LogError 日志"); } }
.net core 控制台程序实例:
class Program { static void Main(string[] args) { var logger = new LoggerFactory().AddLog4Net().CreateLogger("logs");
logger.LogError($"{DateTime.Now} LogError 日志");
}
}
log4net 配置文件 log4net.config(该配置跟原 .net 有差别,xml 根节点 为 log4net)
<log4net> <root> <level value="ALL" ref="infoAppender" /> <appender-ref ref="infoAppender" /> <appender-ref ref="errorAppender" /> <appender-ref ref="debugAppender" /> </root> <!-- 日志的等级,它们由高到底分别为: OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL --> <!--信息日志配置--> <appender name="infoAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Logs\Info\info.log" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="PreserveLogFileNameExtension" value="true" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyyMMdd" /> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="INFO" /> <param name="LevelMax" value="INFO" /> </filter> </appender> <!--调试日志配置--> <appender name="debugAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Logs\Debug\debug.log" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="PreserveLogFileNameExtension" value="true" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyyMMdd" /> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="DEBUG" /> <param name="LevelMax" value="DEBUG" /> </filter> </appender> <!--错误日志配置--> <appender name="errorAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Logs\Error\Err.log" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="PreserveLogFileNameExtension" value="true" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyyMMdd" /> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="ERROR" /> <param name="LevelMax" value="ERROR" /> </filter> </appender> </log4net>