.netcore配置log4net方法

方法一:

1.通过Nuget日志包log4net.aspnetcore

2.在Program中的CreateHostBuilder方法中添加如下代码

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            //引用log4net.aspnetcore写日志,筛选部分日志
            .ConfigureLogging((context, loggingBuilder) =>
            {
                loggingBuilder.AddFilter("Microsoft", LogLevel.Warning);
                loggingBuilder.AddFilter("System", LogLevel.Warning);
                //默认根目录路径
                //loggingBuilder.AddLog4Net();

                //指定路径
                var path = System.IO.Directory.GetCurrentDirectory();
                loggingBuilder.AddLog4Net($"{path}/log4netcore.config");
            })

            //未筛选,直接显示全部日志
            //.ConfigureLogging(p =>
            //{
            //    var path = System.IO.Directory.GetCurrentDirectory();
            //    p.AddLog4Net($"{path}/log4netnetcore.config");//配置文件                    
            //})

            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });

3.log4netcore.config文件

<?xml version="1.0" encoding="utf-8" ?>

  <!-- 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/log.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>

 

方法二:通过类库写日志

1.通过Nuget日志包log4net

2.在Startup中添加如下代码

//配置log4net写日志
        public static ILoggerRepository repository { get; set; }
        //public Startup(IConfiguration configuration)
        //{
        //    Configuration = configuration;
        //}

        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                //.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
            repository = LogManager.CreateRepository("NETCoreRepository");
            XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));


            WriteLog.LogRepository = repository;//类库中定义的静态变量

        }

3.在类库中添加如下代码

public class WriteLog
    {
        public static ILoggerRepository LogRepository { get; set; }
    }

    public class Base
    {
        public static readonly ILog log = LogManager.GetLogger(WriteLog.LogRepository.Name,typeof(Base));

        public static void TestLog()
        {
            log.Info("Info Test");
        }
    }

4.log4net.config的内容比上面log4netcore.config多一个节点

<configuration>

.....

</configuration>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值