微软日志工厂 Microsoft.Extensions.Logging 中增加 log4net 的日志输出

前提:

  需要nuget   Microsoft.Extensions.Logging.Log4Net.AspNetCore   2.2.6;

 

描述:解决 .net core 微软日志工厂 Microsoft.Extensions.Logging  增加 log4net 的日志输出,暂时分为 info,Error,Debug 三种类型,按天记录;

 

实际效果:

微软日志工厂 Microsoft.Extensions.Logging 中增加 log4net 的日志输出_第1张图片

微软日志工厂 Microsoft.Extensions.Logging 中增加 log4net 的日志输出_第2张图片

 

.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()
                .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
    {
        /// 
        /// 日志信息
        /// 
        ILogger logger;

        /// 
        /// 构造函数配置日志
        /// 
        /// 
        public HomeController(ILogger 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>

  
  
  <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>

 

你可能感兴趣的:(微软日志工厂 Microsoft.Extensions.Logging 中增加 log4net 的日志输出)