无法识别的配置部分log4net

时间:2013-10-17 06:18:17

标签: c# asp.net log4net

我在web.config中有这段代码:

<log4net>
  <root>
    <level value="ALL" />
    <appender-ref ref="LogFileAppender" />
  </root>
  <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="D:\logFileFaculty.txt" />
    <param name="AppendToFile" value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>
</log4net>

我已下载log4net.dll并将其放入Bin文件夹。

在我的aspx.cs个网页中,我添加了以下代码:

using log4net;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]


private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

但它的错误为Unrecognized configuration section log4net

3 个答案:

答案 0 :(得分:72)

您需要声明log4net部分:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

仔细查看documentation,它解释了必要的事情。

答案 1 :(得分:0)

<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
    </configSections>
</configuration>

添加到您的app.config文件

答案 2 :(得分:0)

不确定为什么不发布整个示例。

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  ...
  <log4net>
    ...
  </log4net>
</configuration>