码迷,mamicode.com
首页 > Windows程序 > 详细

C# log4net

时间:2020-01-03 12:07:14      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:oid   ref   pattern   backup   logger   assembly   manager   conf   rollback   

1.install-package log4net

2.add new config file, its name is log4net.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="console" />
      <appender-ref ref="file" />
    </root>
    <appender name="console" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level %logger - %message%newline" />
      </layout>
    </appender>
    <appender name="file" type="log4net.Appender.RollingFileAppender">
      <file value="myapp.log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
    </appender>
  </log4net>
</configuration>

3.Right click the log4net.config and set Copy to Output Directory as ‘Copy Always‘

4.Open AssemblyInfo.cs and append to the end

[assembly:log4net.Config.XmlConfigurator(ConfigFile ="log4net.config")]

5.

private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
Log4netDemo();
}

static void Log4netDemo()
{
logger.Info($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
logger.Error($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
logger.Debug($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
logger.Fatal($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
logger.Warn($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
}

 

C# log4net

标签:oid   ref   pattern   backup   logger   assembly   manager   conf   rollback   

原文地址:https://www.cnblogs.com/Fred1987/p/12144034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!