Log4Net配置XML

时间:2010-07-20 17:51:26

标签: c# logging log4net log4net-configuration

我的App.config如下。该文件与调用的项目相同:

log4net.Config.XmlConfigurator.Configure();
private static log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

但是我收到了:

  

Log4net:错误XmlConfigurator无法在应用程序的.config文件中找到配置节'log4net'。检查.config文件中的<log4net><configSections>元素。配置部分应如下所示:<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /

我觉得我犯了一个愚蠢的错误,因为我刚开始使用log4net。对此有何帮助?

<!-- language: xml -->

    <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
  <configSections>
<section name="log4net" 
         type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
 </configSections>

 <log4net>

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="C:\log-file.txt" />
  <appendToFile value="true"/>
  <rollingStyle value="Size" />
  <maximumFileSize value="1MB" />
  <staticLogFileName value="true" />
  <maxSizeRollBackups value="10" />
  <conversionPattern value="%date [%thread] %-5level %location %logger -    %message%newline" />
  <layout type="log4net.Layout.SimpleLayout" />
</appender>

<root>
       <level value="ALL" />
       <appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>

1 个答案:

答案 0 :(得分:6)

我在web.config中唯一看到的就是你错过了结束标记。