使用log4net-1.2.0-beta8
1>在项目的AssemblyInfo.cs文件中设置。。。这一步如果忘了就监视不到日志文件了
[assembly: log4net.Config.DOMConfiguratorAttribute(Watch=true)]
或者
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
2>添加并编辑项目配置文件(默认为App.config),内容如下:
内容一:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="stdout" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<!--conversionPattern value="[%d %l] %n%m%n" /-->
<param name="ConversionPattern" value="[%d %l] %m%n" />
</layout>
</appender>
<appender name="file" type="log4net.Appender.FileAppender">
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="[%d %l] %m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="stdout" />
<appender-ref ref="file" />
</root>
<logger name="*">
<level value="ALL" />
<appender-ref ref="stdout" />
<appender-ref ref="file" />
</logger>
</log4net>
</configuration>
【或者内容二:】
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net debug="false">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="log/OperationLog.txt" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd'.txt'" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{yyyy-MM-dd HH:mm:ss}% %-5level %message%newline"/>
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
</configuration>
(内容二可以直接复制后使用)
程序代码中使用如下语句
...
using log4net;
using log4net.Config;
...
private static ILog log = LogManager.GetLogger(typeof(TestClass));
...
log.Info("This is a test!");