18

This is my error message:

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" />

this is my web.config:

<?xml version="1.0"?>
<configuration>
    <configSections>
       <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>

   <system.serviceModel>
   ...
   </system.serviceModel>

   <connectionStrings>
   ...
   </connectionStrings>

   <log4net>
   ...
   </log4net>

</configuration>

What is wrong with my config?

Update:

Have also Web.Release.config:

<?xml version="1.0"?>    
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

    <system.web>
        <compilation xdt:Transform="RemoveAttributes(debug)" />
    </system.web>

    <system.serviceModel>
    ...
    </system.serviceModel>

    <connectionStrings>
    ...
    </connectionStrings>

    <log4net>
    ...   
       <root>
          <level value="DEBUG" xdt:Transform ="Replace"/>
       </root>
    </log4net>

</configuration>

Web.Test.cofig - the same as Release one

and Web.Debug.config, that is empty:

<?xml version="1.0"?>

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">    

</configuration>
MikroDel
  • 6,705
  • 7
  • 39
  • 74

2 Answers2

28

Are you calling XmlConfigurator.Configure() somewhere?

Remove those calls and only add the [assembly: log4net.Config.XmlConfigurator(Watch = true)] attribute.

Normally it is easier to configure log4net in a separate file. Create a file log4net.config and change your attribute to:

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

Remove the section in your web.config.

ErikE
  • 48,881
  • 23
  • 151
  • 196
Peter
  • 27,590
  • 8
  • 64
  • 84
  • "Are you calling XmlConfigurator.Configure() somewhere?" - no – MikroDel Jul 04 '13 at 11:38
  • This worked. I guess XmlConfigurator.Configure() isn't used anymore?? This is how I've always done it. I never had to modify the assembly.cs. I better read the latest documentation. – goku_da_master Sep 02 '15 at 19:30
  • Thank you, this worked, however oddly, I had that line (log4net.Config.XmlConfigurator.Configure();) in another program and I did not get the log4net error. However, I feel this is a legitimate solution to the OP issue. – j.hull May 22 '19 at 19:16
6

Nothing seems wrong with defining the section under <configSections>.

Try adding [assembly: log4net.Config.XmlConfigurator(Watch = true)] in your AssemblyInfo.cs in the properties folder of your project. This should do the trick in case your configuration is correct under the tag.

EDIT :

XmlElement log4NetSection = (XmlElement)ConfigurationManager.GetSection("log4net");
            log4net.Config.XmlConfigurator.Configure(log4NetSection);
Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81
sm_
  • 2,572
  • 2
  • 17
  • 34
  • thanks for your answer Siraj. Tried it and nothing happend - the same error message – MikroDel Jul 04 '13 at 11:26
  • Check my Edit and try this piece of code on your "AppStart" or the point execution starts on your application – sm_ Jul 04 '13 at 12:04
  • Don't forget to add reference to the system.configuration assembly. and add "using System.Configuration" – sm_ Jul 04 '13 at 12:05
  • It all doesnt helped me, but as I see my Logger works fine event with errot message. But to solve it will be fine ) – MikroDel Nov 01 '13 at 11:06
  • @SirajMansour 's EDIT was the solution for me. I had to do this after upgrading from .NET 4.0 to 4.5.2. – realmikep Dec 03 '15 at 01:26