【问题标题】:log4net configuration - failed to find sectionlog4net 配置 - 找不到部分
【发布时间】:2013-07-02 08:58:17
【问题描述】:

这是我的错误信息:

log4net:ERROR XmlConfigurator: Failed to find configuration section 'log4net' in the application's .config file. Check your .config file for the <log4net> and <configSections> elements. The configuration section should look like: <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

这是我的 web.config:

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

   <system.serviceModel>
   ...
   </system.serviceModel>

   <connectionStrings>
   ...
   </connectionStrings>

   <log4net>
   ...
   </log4net>

</configuration>

我的配置有什么问题?

更新:

还有Web.Release.config:

<?xml version="1.0"?>    
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

    <system.web>
        <compilation xdt:Transform="RemoveAttributes(debug)" />
    </system.web>

    <system.serviceModel>
    ...
    </system.serviceModel>

    <connectionStrings>
    ...
    </connectionStrings>

    <log4net>
    ...   
       <root>
          <level value="DEBUG" xdt:Transform ="Replace"/>
       </root>
    </log4net>

</configuration>

Web.Test.cofig - 与 Release one 相同

和 Web.Debug.config,即为空:

<?xml version="1.0"?>

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">    

</configuration>

【问题讨论】:

  • 我收到此错误消息,但我的 Logger 工作正常。

标签: c# .net logging log4net


【解决方案1】:

你在某处打电话给XmlConfigurator.Configure() 吗?

删除这些调用,只添加[assembly: log4net.Config.XmlConfigurator(Watch = true)] 属性。

通常在单独的文件中配置 log4net 更容易。创建一个文件log4net.config 并将您的属性更改为:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

删除web.config 中的部分。

【讨论】:

  • "你在某处调用 XmlConfigurator.Configure() 吗?" - 没有
  • 这行得通。我猜 XmlConfigurator.Configure() 不再使用了??这就是我一直这样做的方式。我从来不需要修改 assembly.cs。我最好阅读最新的文档。
  • 谢谢,这行得通,但奇怪的是,我在另一个程序中有那行 (log4net.Config.XmlConfigurator.Configure();),但我没有收到 log4net 错误。但是,我觉得这是 OP 问题的合法解决方案。
【解决方案2】:

&lt;configSections&gt; 下定义部分似乎没有错。

尝试在项目属性文件夹的 AssemblyInfo.cs 中添加 [assembly: log4net.Config.XmlConfigurator(Watch = true)]。如果标签下的配置正确,这应该可以解决问题。

编辑:

XmlElement log4NetSection = (XmlElement)ConfigurationManager.GetSection("log4net");
            log4net.Config.XmlConfigurator.Configure(log4NetSection);

【讨论】:

  • 感谢您的回答 Siraj。试过了,什么也没发生——同样的错误信息
  • 检查我的编辑并在您的“AppStart”上尝试这段代码或在您的应用程序上开始执行点
  • 不要忘记添加对 system.configuration 程序集的引用。并添加“使用 System.Configuration”
  • 这一切都对我没有帮助,但正如我所见,我的 Logger 工作正常,带有错误消息。但是解决它就可以了)
  • @SirajMansour 的 EDIT 是我的解决方案。从 .NET 4.0 升级到 4.5.2 后,我必须这样做。
猜你喜欢
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
相关资源
最近更新 更多