C# 让 log4net 使用应用程序配置文件获取配置数据

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

Have log4net use application config file for configuration data

c#logginglog4net

提问by laconicdev

I would like to store log4net config data in my application.config file. Based on my understanding of the documentation, I did the following:

我想将 log4net 配置数据存储在我的 application.config 文件中。根据我对文档的理解,我做了以下事情:

  1. Add a reference to log4net.dll

  2. Add the following line in AssemblyInfo.cs:

    [assembly: log4net.Config.XmlConfigurator(Watch = true)]
    
  3. Initialize the logger as follows:

    private static readonly ILog log = LogManager.GetLogger(typeof(frmWizard));
    
  4. I have the following code in my app.config:

  1. 添加对 log4net.dll 的引用

  2. 在 AssemblyInfo.cs 中添加以下行:

    [assembly: log4net.Config.XmlConfigurator(Watch = true)]
    
  3. 初始化记录器如下:

    private static readonly ILog log = LogManager.GetLogger(typeof(frmWizard));
    
  4. 我的 app.config 中有以下代码:

    <configSections>
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
      <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
      </appender>
      <root>
        <level value="INFO" />
        <appender-ref ref="ConsoleAppender" />
      </root>
    </log4net>

However, when I run the application, I get the following error on the console:

但是,当我运行该应用程序时,在控制台上出现以下错误:

No appender named [Consoleappender] could be found.

找不到名为 [Consoleappender] 的附加程序。

How can I get log4net to read settings from the config file?

如何让 log4net 从配置文件中读取设置?

Thanks!

谢谢!

采纳答案by Charles Bretana

Add a line to your app.config in the configSections element

在 configSections 元素中向 app.config 添加一行

<configSections>
 <section name="log4net" 
   type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0, 
         Culture=neutral, PublicKeyToken=1b44e1d426115821" />
</configSections>

Then later add the log4Net section, but delegate to the actual log4Net config file elsewhere...

然后稍后添加 log4Net 部分,但委托给其他地方的实际 log4Net 配置文件...

<log4net configSource="Config\Log4Net.config" />

In your application code, when you create the log, write

在您的应用程序代码中,当您创建日志时,写入

private static ILog GetLog(string logName)
{
    ILog log = LogManager.GetLogger(logName);
    return log;
}

回答by Joachim Kerschbaumer

Have you tried adding a configsectionhandler to your app.config? e.g.

您是否尝试向configsectionapp.config添加处理程序?例如

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

回答by Konstantin

From the config shown in the question there is but one appender configured and it is named "EventLogAppender". But in the config for root, the author references an appender named "ConsoleAppender", hence the error message.

从问题中显示的配置中,只配置了一个 appender,它被命名为“EventLogAppender”。但是在 root 的配置中,作者引用了一个名为“ConsoleAppender”的 appender,因此出现了错误消息。

回答by Shekhar

All appender names must be reflected in the root section.
In your case the appender name is EventLogAppenderbut in the <root> <appender-ref ..section it is named as ConsoleAppender. They need to match.

所有 appender 名称都必须反映在根部分中。
在您的情况下,appender 名称是EventLogAppender,但在该<root> <appender-ref ..部分中,它被命名为ConsoleAppender。他们需要匹配。

You can add multiple appenders to your log config but you need to register each of them in the <root>section.

您可以将多个 appender 添加到您的日志配置中,但您需要在该<root>部分中注册它们中的每一个。

<appender-ref ref="ConsoleAppender" />
<appender-ref ref="EventLogAppender" />

You can also refer to the apache documentationon configuring log4net.

您也可以参考 apache文档配置 log4net。

回答by Nick N.

I fully support @Charles Bretana's answer. However, if it's not working, please make sure that there is only one <section>element AND that configSectionsis the first child of the root element:

我完全支持@Charles Bretana 的回答。但是,如果它不起作用,请确保只有一个<section>元素并且它configSections根元素第一个子元素

configsectionsmust be the first element in your app.Configafter configuration:

configsections必须是app.Configafter 配置中的第一个元素:

<?xml version="1.0"?encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net"  type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" />
  </configSections>
  <!-- add log 4 net config !-->
  <!-- add others e.g. <startup> !-->
</configuration>