1、Nuget安装,当前版本2.0.8
<?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>
注意:要在文件属性中将“复制到输出目录”选项设置为“如果较新则复制”
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config",Watch = true)]
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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。