.net core日志记录

.net core日志记录

日志是必须的,目前采用log4net进行日志记录。

定义通用的日志记录方法

public static class Log4NetFunc
{
    private static log4net.ILog logger = null;
    private static object locker = new object();

    private static void InitLogger()
    {
        lock (locker)
        {
            if (logger == null)
            {
                ILoggerRepository repository = LogManager.CreateRepository("repos");
                XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
                logger = log4net.LogManager.GetLogger(repository.Name, "logger");
            }
        }
    }

    public static log4net.ILog LOGGER
    {
        get
        {
            if (logger == null)
            {
                InitLogger();
            }
            return logger;
        }
    }
}

配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-1.0"/>
  </configSections>
  <log4net>
    <!--console log-->
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
    </appender>
    <root name="logger">
      <level value="ALL" />
      <appender-ref ref="ConsoleAppender" />
    </root>
  </log4net>
</configuration>

日志记录

Log4NetFunc.LOGGER.Debug("程序已启动");
上一篇:URI与URL的区别


下一篇:HTTP 协议中 URI 和 URL 有什么区别?