.net core Log4net配置和使用

1.首先安装 Log4net

Install-Package log4net -Version 2.0.8

2.项目目录中新增Log4net配置文件log4net.config

file:日志文件的默认位置

dataPatten:日志文件名称

下面的配置文件配置的路径是:项目根目录\logs\日志类型\yyyyMMdd\yyyyMMdd_hh.log



  
    

3.日志帮助类

public class LogHelper
    {
        private static ILoggerRepository repository { get; set; }
        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, "RollingLogFileAppender");
        }
        public static void Info(string msg)
        {
            log.Info(msg);
        }

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

        public static void Error(string msg, Exception exception = null)
        {
            log.Error(msg, exception);
        }

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

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

4.使用

LogHelper.Info("测试日志")

你可能感兴趣的:(C#,日志,Log4net,c#)