.net core 使用 log4net

1 新建 .net 6 webapi 项目

2 安装nuget包 log4net

3 新建工具类 Log4NetUtil

添加引用

using System.IO;
using log4net;
using log4net.Config;
using log4net.Repository;

 

    public class Log4NetUtil
    {
        private static ILoggerRepository _repository;
        private static ILog _log;

        private static ILog log
        {
            get
            {
                if (_log == null)
                {
                    Configure();
                }
                return _log;
            }
        }

        public static void Configure(string repositoryName = "NETCoreRepository", string configFile = "log4net.config")
        {
            _repository = LogManager.CreateRepository(repositoryName);
            XmlConfigurator.Configure(_repository, new FileInfo(configFile));
            _log = LogManager.GetLogger(repositoryName, "");
        }

        public static void Info(string msg)
        {
            log.Info(msg);
        }

        public static void Warn(string msg)
        {
            log.Warn(msg);
        }

        public static void Error(string msg)
        {
            log.Error(msg);
        }
    }

4 添加 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 %newline%message%newline%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 %newline%message%newline%newline" />
      </layout>
    </appender>

    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="log4file/" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <staticLogFileName value="false" />
      <datePattern value="yyyyMMdd'.log'" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %newline%message%newline%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>

设置 log4net.config 文件属性

 

 5 测试

写日志代码

        public IEnumerable<WeatherForecast> Get()
        {
            Log4NetUtil.Info("这是 info");
            Log4NetUtil.Warn("这是 Warn");
            Log4NetUtil.Error("这是 Error");

            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }

5.1 debug模式下查看日志,进入 bin\Debug\net6.0\log4file 目录下

 

 

 

 5.2 发布运行后查看方式

 

 

参考链接:https://www.cnblogs.com/zhengyb/p/13859090.html

posted @ 2022-06-15 17:04  温故纳新  阅读(1378)  评论(0编辑  收藏  举报