Have log4net use application config file for configuration data

前端 未结 5 1150
北恋
北恋 2020-12-23 19:34

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

  1. Add a referenc

相关标签:
5条回答
  • 2020-12-23 19:56

    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 configSections is the first child of the root element:

    configsections must be the first element in your app.Config after configuration:

    <?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>
    
    0 讨论(0)
  • 2020-12-23 20:02

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

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

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

    You can also refer to the apache documentation on configuring log4net.

    0 讨论(0)
  • 2020-12-23 20:07

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

    <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 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;
    }
    
    0 讨论(0)
  • 2020-12-23 20:17

    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.

    0 讨论(0)
  • 2020-12-23 20:20

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

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    
    0 讨论(0)
提交回复
热议问题