log4net配置 - 找不到部分

时间:2013-07-04 11:13:42

标签: c# .net logging log4net

这是我的错误消息:

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 - 与第一版

相同

和Web.Debug.config,它是空的:

<?xml version="1.0"?>

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

</configuration>

2 个答案:

答案 0 :(得分:24)

你在某个地方打电话给XmlConfigurator.Configure()吗?

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

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

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

删除web.config

中的部分

答案 1 :(得分:6)

<configSections>下定义部分似乎没有错。

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

编辑:

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