C# 为什么在配置文件中无法识别 log4net?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17106682/
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 08:36:40  来源:igfitidea点击:

Why is log4net not recognized in configuration file?

c#log4net

提问by John Threepwood

I wrote a test console application in C# using log4net:

我使用log4net在 C# 中编写了一个测试控制台应用程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using log4net;
using log4net.Config;

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

namespace Log4Net_Test
{
    class Program
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Program));

        static void Main(string[] args)
        {    
            log.Info("Entering application");    
            log.Debug("Debug message");    
            log.Info("Leaving application");
        }
    }
}

My App.configfile looks like this:

我的App.config文件看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
  </appSettings>
  <log4net>
    <!-- A1 is set to be a ConsoleAppender -->
    <appender name="A1" type="log4net.Appender.FileAppender">
      <file value="logfile.txt" />
      <appendToFile value="false" />

      <!-- A1 uses PatternLayout -->
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
      </layout>
    </appender>

    <!-- Set root logger level to DEBUG and its only appender to A1 -->
    <root>
      <level value="DEBUG" />
      <appender-ref ref="A1" />
    </root>
  </log4net>
</configuration>

Executing the test program ends up in the following error message:

执行测试程序最终会出现以下错误消息:

log4net:ERROR Exception while reading ConfigurationSettings. Check your .config
file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed t
o initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognize
d configuration section log4net. 

What is wrong with my configuration file?

我的配置文件有什么问题?

UPDATE 1:The configSections′ part was missing, as pointed out in the accepted answer. But I also had to remove thestartupsection, otherwise the same error appeared. I do not know why thestartup` section is causing the problem, too. Perhaps someone more experienced can tell and write a comment.

更新1:configSections′ part was missing, as pointed out in the accepted answer. But I also had to remove the启动section, otherwise the same error appeared. I do not know why thestartup`部分导致了问题,太。也许更有经验的人可以告诉并写评论。

采纳答案by Massimiliano Peluso

You need to add log4netalso in the section block

您还需要log4net在部分块中添加

<configSections>

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

</configSections>

回答by Milen Kindekov

The startup section needed to be removed as well because log4netdoes not support Framework .NET4.5

启动部分也需要删除,因为log4net不支持Framework .NET4.5

You can see my similar answer here: https://stackoverflow.com/a/13236410/1783224

你可以在这里看到我的类似答案:https: //stackoverflow.com/a/13236410/1783224

Or you can see the supported frameworks in the documentation: http://logging.apache.org/log4net/release/features.html

或者您可以在文档中查看支持的框架:http: //logging.apache.org/log4net/release/features.html

回答by elcudro

You should also remember that section block <configSections> must be under the <configuration> otherwise will occurred an error:

您还应该记住部分块 < configSections> 必须在 < configuration>下,否则会发生错误:

Exception while reading ConfigurationSettings. Check your .config file is well formed XML.

读取 ConfigurationSettings 时出现异常。检查您的 .config 文件是格式正确的 XML。

For example:

例如:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>   
  <!-- Log4net Logging Setup -->   
  <log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
      <file value="service.log" />
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <!--
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
      -->
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%date [%thread] %-5level %logger.%method - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="FATAL" />
      </filter>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="FileAppender" />
    </root>   
  </log4net>   
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
</configuration>