53

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 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:

    <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.

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

Thanks!

Philipp M
  • 1,877
  • 7
  • 27
  • 38
laconicdev
  • 6,360
  • 11
  • 63
  • 89
  • What code do you have in your app.config? – sgwill Dec 16 '08 at 21:11
  • 4
    Note to others: The `app.config` settings shown are, apparently, correct *except* for `EventLogAppender` being named in the `` section, and `ConsoleAppender` being named in the `` section - which do not match. See @Konstantin's answer. Also, **for others unfamiliar with log4net** using this question to learn how to use it - **note that you will probably want a different *type* of appender** than the one here - such as `type="log4net.Appender.FileAppender,log4net"` (which appends to a file, not to the Windows Event Log). – Dan Nissenbaum Jan 28 '14 at 18:13

5 Answers5

40

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;
}
Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
  • A side note, I was also missing `[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]` Specifically **ConfigFile = "Log4Net.config"**. – Matt Canty Jul 31 '14 at 14:26
37

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.

Konstantin
  • 3,626
  • 2
  • 33
  • 45
  • 1
    This should be the accepted answer as the OP wants to merge `log4net.config` and `app.config` files of his application into one. – RBT Jul 08 '21 at 08:35
5

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>
Nick N.
  • 12,902
  • 7
  • 57
  • 75
3

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

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Joachim Kerschbaumer
  • 9,695
  • 7
  • 49
  • 84
2

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.

Shashank Shekhar
  • 3,958
  • 2
  • 40
  • 52