配置部分"log4net"缺少一个部分声明。

19

我正在尝试将log4net添加到我的MVC5项目中。我已经执行了以下步骤:

Install-Package log4net

这似乎已经成功安装了log4net。

我在web.config的configuration部分中添加了以下内容:

<log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="Logs\ApiLog.txt" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="10MB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
           <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
        </layout>
    </appender>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="RollingLogFileAppender" />
    </root>
</log4net>

我在web.config文件的configSections中添加了以下内容;

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
我已经将以下内容添加到了我的Global.asax.cs文件中:
log4net.Config.XmlConfigurator.Configure();

这个解决方案已编译,但当我尝试运行程序时出现以下错误:

HTTP 错误 500.19 - 内部服务器错误

由于页面相关的配置数据无效,所请求的页面无法访问。

配置部分 'log4net' 无法读取,因为缺少部分声明。

请问有什么建议或想法吗?


请参考此链接:https://dev59.com/fmIk5IYBdhLWcg3wRcUo - Shreyas Achar
log4net 部分是否放置在 configSections 部分之后? - David
@David,由于某些原因,该程序包似乎没有通过Nuget正确配置。运行update-package -reinstall后,这似乎清除了错误。感谢您的回复。 - Matthew Flynn
可能是Unrecognized configuration section log4net的重复问题。 - Mikael Dúi Bolinder
3个回答

28

你需要在web.config中包含这个内容。

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

应该看起来像下面这样,同时确保你的应用程序bin文件夹中有log4net.dll、log4net.xml。

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <appSettings>    
  </appSettings>
  <log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logfile.log" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value=".yyyyMMdd.lo\g" />
      <maximumFileSize value="5MB" />
      <maxSizeRollBackups value="-1" />
      <countDirection value="1" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level [%thread] [%aspnet-session{SessionId}] %logger - %message%newline%exception" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>

Rolwin,我已经更新了我的答案,正如你所看到的,这已经存在。 - Matthew Flynn

8

您可能忘记声明配置部分。请在web.config中尝试以下内容:

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

嗨Kay,我已经编辑了上面的内容,就像发布的那样,请问有什么问题吗? - Matthew Flynn

5
注意:在 configsections 中,您需要将部分名称放在 sectionGroup 之外。
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
</sectionGroup>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接