netcore 使用log4net

1.Install

Install-Package log4net

2. conifg

創建文件:log4net.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!-- This section contains the log4net configuration settings -->
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
    </appender>

    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="log-file.log" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>

    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logfile/" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <staticLogFileName value="false" />
      <datePattern value="yyyyMMdd'.log'" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="1MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>

    <!-- Setup the root category, add the appenders and set the default level -->
    <root>
      <level value="ALL" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="FileAppender" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>

  </log4net>
</configuration>

對應Startup中的配置:

log4net.Config.XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));

3.Startup

public class Startup
{
    public static log4net.Repository.ILoggerRepository repository { get; set; }


    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("Sigmal.json", optional: false, reloadOnChange: true)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();

        repository = log4net.LogManager.CreateRepository("NETCoreRepository");
        log4net.Config.XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
    }

    public IConfigurationRoot Configuration { get; }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {

        //...
        var log = log4net.LogManager.GetLogger(repository.Name, typeof(Startup));
        log.Info("test log4net");
        //...
    }
}

4.Usage

使用經典的LogManager獲取日誌對象:

var log = log4net.LogManager.GetLogger(repository.Name, typeof(ToDoController));
log.Info("test log4net");
posted @ 2017-05-27 13:31 _DN 阅读(...) 评论(...) 编辑 收藏