log4net快速使用流程

以下内容大部分来自这里,对原作者流子表示感谢

1、Nuget安装,当前版本2.0.8

2、创建log4net.config文件文件内容如下:

分享图片

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <log4net debug="true">
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
      <appender-ref ref="ConsoleAppender" />
    </root>
    <appender name="ConsoleAppender"  type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.PatternLayout">
       <conversionPattern value="[%date] %level [%thread][%c{1}:%line] - %m%n" />
      </layout>
    </appender>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <param name="File" value="game" />
      <param name="DatePattern" value="‘.‘yyyy-MM-dd-HH‘.log‘" />
      <param name="AppendToFile" value="true" />
      <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="[%date] %level [%thread][%c{1}:%line] - %m%n" />
      </layout>
    </appender>
  </log4net>
</configuration>
View Code

注意:要在文件属性中将“复制到输出目录”选项设置为“如果较新则复制”

3、在文件AssemblyInfo.cs中加上

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

4、代码用法

using log4net;
private static readonly ILog logger = LogManager.GetLogger(typeof(GameClient));  
Thread.CurrentThread.Name = "main";//为了将主线程名与其他线程区别开
//初始化log4net
log4net.Config.XmlConfigurator.Configure();
logger.Debug("初始化连接开始");
logger.InfoFormat("测试日志 name={0}","client");

5、输出结果:

[2017-08-11 20:18:19,350] DEBUG [main][GameClient:24] - 初始化连接开始
[2017-08-11 20:18:19,363] DEBUG [main][GameClient:28] - 初始化连接完毕
[2017-08-11 20:18:19,364] INFO [main][SuperSocketDemo:35] - 测试日志 name=client

 UPDAT:

不知道为什么代码不能编辑了,只能暂时放在下面。

如果需要通过DebugView这类的工具查看Trace信息,配置内容如下:

分享图片

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <log4net debug="true">
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="TraceAppender" />
    </root>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <param name="File" value="LoadOrderInterface.log" />
      <param name="DatePattern" value="‘.‘yyyy-MM-dd-HH‘.log‘" />
      <param name="AppendToFile" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%date] %level [%thread][%c{1}:%line] - %m%n" />
      </layout>
    </appender>
    <appender name="ConsoleAppender"  type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%date] %level [%thread][%c{1}:%line] - %m%n" />
      </layout>
    </appender>
    <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level [%thread] %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
  </log4net>
</configuration>
View Code

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念