C# log4net 配置 - 未能找到部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17468841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:34:00  来源:igfitidea点击:

log4net configuration - failed to find section

c#.netlogginglog4net

提问by MikroDel

This is my error message:

这是我的错误信息:

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" />

this is my web.config:

这是我的 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>

What is wrong with my config?

我的配置有什么问题?

Update:

更新:

Have also Web.Release.config:

也有 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 - the same as Release one

Web.Test.cofig - 与第一版相同

and Web.Debug.config, that is empty:

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

<?xml version="1.0"?>

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

</configuration>

回答by Siraj Mansour

Nothing seems wrong with defining the section under <configSections>.

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

Try adding [assembly: log4net.Config.XmlConfigurator(Watch = true)]in your AssemblyInfo.cs in the properties folder of your project. This should do the trick in case your configuration is correct under the tag.

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

EDIT :

编辑 :

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

回答by Peter

Are you calling XmlConfigurator.Configure()somewhere?

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

Remove those calls and only add the [assembly: log4net.Config.XmlConfigurator(Watch = true)]attribute.

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

Normally it is easier to configure log4net in a separate file. Create a file log4net.configand change your attribute to:

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

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

Remove the section in your web.config.

删除web.config.