18

I am using log4net for logging. My logging configuration is stored in a separate file.

Web.Config:ConfigSections

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

Specifying my config file in AssemblyInfo.cs

[assembly: log4net.Config.XmlConfigurator(ConfigFile="Log4Net.config", Watch = true)] 

And when I initialize my LogManager, I get this error

"System.TypeLoadException"
message: Could not load type 'log4net.Config.Log4NetConfigurationSectionHlandler' from assembly 'Log4net'.

Yes it says "Log4NetConfigurationSectionHlandler'", it is not a typo

and later, this error

An error occurred creating the configuration section handler for log4net: Could not load type 'log4net.Config.Log4NetConfigurationSectionHlandler' from assembly 'Log4net'. 

Edit: Tried Mauricio Scheffer's suggestion

got

log4net:ERROR XmlConfigurator: Failed to find configuration section 'log4net' in the application's .config file. Check your .config file for the <log4net> and <configSections> elements. The configuration section should look like: <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
tshepang
  • 12,111
  • 21
  • 91
  • 136
ram
  • 11,468
  • 16
  • 63
  • 89
  • duplicate: http://stackoverflow.com/questions/1321261/configuring-log4net-with-xml-file – Mauricio Scheffer Dec 18 '09 at 20:09
  • Edit:I just found that my web.config had a type "Log4NetConfigurationSectionHlandler" was from web.config. I am still working on getting this work with an external config file – ram Dec 18 '09 at 20:35

1 Answers1

27

If you have your config in a separate log4net.config file you don't need the sectionHandler. Remove it.

You're also probably calling XmlConfigurator.Configure() somewhere in your code. Remove that as well.

Also see this question

Community
  • 1
  • 1
Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • I second this response. I was getting the same problem in my WPF application when I had the log4net section in my `app.config` file. I moved the config to a separate log4net.config file and it resolved the issue. – Rob Sobers Dec 18 '09 at 19:19
  • 1
    I got this error on trying this suggestion: log4net:ERROR XmlConfigurator: Failed to find configuration section 'log4net' in the application's .config file. Check your .config file for the and elements. The configuration section should look like:
    – ram Dec 18 '09 at 19:29
  • 1
    @ram: you're probably calling XmlConfigurator.Configure() somewhere in your code. remove that. – Mauricio Scheffer Dec 18 '09 at 20:06
  • I am a bit lost mauricio, I am calling it when I initialize my Log Manager static ActivityLogManager() { log4net.Config.XmlConfigurator.Configure(); } It should not be called ? – ram Dec 18 '09 at 20:39
  • OK now I get it, since my config file is external, I should not call XMLConfigurator.Configure as it is looking for the config section in my web/app.config. Is my understanding correct ? – ram Dec 18 '09 at 20:48
  • 4
    Exactly, you should *not* call it. The assembly attribute already handles the initialization. – Mauricio Scheffer Dec 18 '09 at 20:59
  • Hey Mauricio, should the external config file be ".xml" or can it have ".config" extension ? – ram Dec 18 '09 at 21:12
  • @ram doesn't matter, it can be anything. – Mauricio Scheffer Dec 18 '09 at 21:13
  • Thanks Mauricio. Am working on it, will update this post ASAP – ram Dec 19 '09 at 21:11
  • Thanks Mauricio it worked. Am posting one new question soon. Its about using one class in am assembly to log all events and I see that "logger" is getting set to this static logging class and not the class which is calling this Log methods – ram Dec 21 '09 at 17:33
  • @Darren : logging configuration should **never** be defined in *libraries*. It's the responsibility of the *application*. – Mauricio Scheffer Jun 22 '12 at 13:42
  • @Darren : it does work without adding anything to the libraries. Your issue is likely elsewhere. Please ask a new question with all relevant details. – Mauricio Scheffer Jun 25 '12 at 03:22
  • New question here: http://stackoverflow.com/questions/11185726/how-to-use-log4net-in-asp-net-from-multiple-assemblies – Darren Jun 25 '12 at 08:23