.Net配置log4net

使用log4net-1.2.0-beta8
在项目的AssemblyInfo.cs文件中设置

[assembly: log4net.Config.DOMConfiguratorAttribute(Watch=true)]
添加并编辑项目配置文件(默认为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>

程序代码中使用如下语句
...
using log4net;
using log4net.Config;
...
private static ILog log = LogManager.GetLogger(typeof(TestClass));
...
log.Info("This is a test!");

ok!收工

你可能感兴趣的:(.net)