采用log4net,使用系统推荐的最新版本:log4net-1.2.11-bin-newkey.zip(网址:http://logging.apache.org/log4net/download_log4net.cgi)
- 将dll添加到项目中
- 在configurationsections结点中添加
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
- 在configuration节点中添加如下内容:
<log4net>
<appender name="SystemAppender" type="log4net.Appender.RollingFileAppender">
<file value="log\\logfile" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyy-MM-dd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender> <root>
<level value="info" />
<appender-ref ref="SystemAppender" />
</root>
</log4net>
- 然后在项目的assembly文件中添加如下内容:
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
- 现在日志环境已经配置成功了, 使用方法如下:
ILog log = LogManager.GetLogger(typeof(MyApp));
log.info("***");