Log4Net Error: Failed to find configuration section 'log4net' in the application's .config file

12,735

Solution 1

You have defined that the log4net section is in your app.config with:

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

that means you should add the content of the log4net.config to the app.config file.

Solution 2

If you're trying to implement a wrapper over log4net where the log4net lives in its own class library and not in the main executable of your application, try something like this in a static constructor of your wrapper class:

static Logger()
{
    log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo
                                            (System.IO.Path.GetDirectoryName

    (System.Reflection.Assembly.GetExecutingAssembly().Location)));
}

The static constructor solution ensures that if you load the class library, the configuration code will run.

You will also need to do something to copy the log4net.config file from that class library project into the same folder as the executable. You can do this by hand when deploying the application; however, to do it automatically when debugging a post-build step with something like this (from the class library project) will put the config file into the right folder:

XCOPY "$(TargetDir)*.config" ".\..\..\..\..\bin\Debug" /S /Y /I
XCOPY "$(TargetDir)*.config" ".\..\..\..\..\bin\Release" /S /Y /I

// Where those paths point to the main executable /bin folders 
// and build configuration target dirs

A good reason to use this approach is that if you're leveraging something like MEF, you can bring in your logger wrapper class as an interface and you don't need to reference log4net in more than one project in your solution. You can interface with the logger with an abstract interface of your own choosing, and use log4net as the Impl. (Bridge pattern)

Good luck!

Share:
12,735
Mason
Author by

Mason

Updated on June 04, 2022

Comments

  • Mason
    Mason almost 2 years

    I currently use a class library to store a class that handles logging using log4net. This class is used in other projects.

    I've read a lot of other questions and answers, but I haven't been able to fix it yet...

    I enabled internal debugging of log4net because it would not write to a file, and this is the error I get:

    log4net:ERROR 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" />
    

    These are my files: log4net.config

    <log4net debug="true">
        <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
            <param name="File" value="Logs/log4net.log" />
            <param name="AppendToFile" value="true" />
            <layout type="log4net.Layout.PatternLayout">
            <param name="Header" value="[Header]\r\n" />
            <param name="Footer" value="[Footer]\r\n" />
            <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
            </layout>
        </appender>
    
        <root>
            <level value="ALL" />
            <appender-ref ref="LogFileAppender" />
        </root>
    </log4net>
    

    App.config

    <?xml version="1.0" encoding="utf-8" ?>
        <configuration>
          <configSections>
            <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
          </configSections>
    
          <appSettings>
            <add key="log4net.Internal.Debug" value="true"/>
          </appSettings>
    
          <parameter>
            <parameterName value="@BlogId" />
            <dbType value="Int32" />
            <layout type="log4net.Layout.RawPropertyLayout">
              <key value="BlogId" />
            </layout>
          </parameter>
        </configuration>
    

    AssemblyInfo.cs (just last line)

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

    Logger.cs

    public class Logger : ILogger
        {
            private static ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    
            public Logger()
            {
                XmlConfigurator.Configure();
                logger.Info("NEW LOGGER INSTANCE CREATED");
            }
    }
    

    UPDATE I fixed it by letting the class that uses my Logger.cs give its own .config file for log4net. Now log4net reads the given .config files and it works.

  • NKAT
    NKAT almost 2 years
    Hello! Chad, would you please elaborate on what code of the static constructor does exactly? My .sln contains 3 projects. (1) Windows.Service, (2) ASP.NET app and (3) shared Class - wrapper for log4net. (1) gets installed in a separate directory and (2) in its own. Where the app gonna look for the .config? Looking forward to read from you