views:

3123

answers:

3

I would like to stored 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)]
  1. Initialize the logger as follows:

    private static readonly ILog log = LogManager.GetLogger(typeof(frmWizard));

  2. I have the following code in my app.config:

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!

A: 

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

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
Joachim Kerschbaumer
+3  A: 

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);
        log4net.Config.XmlConfigurator.Configure();
        return log;
    }
Charles Bretana
LogManager.GetLogger will automatically configure the appdomain when the XmlConfigurator attribute is present in the assembly. Thus, you should not call XmlConfigurator.Configure() since that will cause you to initialize log4net for each logger you're getting.
Peter Lillevold
Thanks just what I needed
Jay
A: 

Charles,

Thank you for your suggestion. The problem was actually "operator headspace" rather than anything wrong on the config part. I copied the log4net example, but I didn't change appender. I am able to use the log4net config section embedded in my config file without any problems.

laconicdev