Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 565
Mason Avatar asked Jan 04 '23 10:01

Mason


2 Answers

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.

like image 98
Peter Avatar answered Jan 21 '23 13:01

Peter


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!

like image 41
Chad Lehman Avatar answered Jan 21 '23 13:01

Chad Lehman