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>
注意:要在文件属性中将“复制到输出目录”选项设置为“如果较新则复制”
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>
Level定义记录的日志级别,表示要记录哪个级别以上的日志,级别由低往高依次是:
ALL
DEBUG
INFO
WARN
ERROR
FATAL
None