2

I'm getting started with log4net.I'm working on a small console project in which i must implement this framework.

At the beginning i created a small console project to see how it works without any other code. I managed to make it work properly.

Now i try to migrate all the code in my application and i get this error when i execute the .exe of my console application:

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

The code:

public class Program
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger
    (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    public static void Main(string[] args)
    {
        log4net.Config.XmlConfigurator.Configure();
        ILog log = log4net.LogManager.GetLogger(typeof(Program));
        ...

The app.config didn't exist yet so its content is the same as the test project i did:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />
    </configSections>
  <log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
      <file value="D:\WEB\SAI\log\nas\log.txt" />
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="FATAL" />
      </filter>
    </appender>

    <root>
      <level value="INFO"/>
      <appender-ref ref="FileAppender"/>
    </root>
  </log4net>
</configuration>

After some searches on internet, i added:

[assembly: log4net.Config.XmlConfigurator()]

to my AssemblyInfo.cs as it was suggested but the result is still the same... Thanks in advance for your help

  • 2
    Try specifying the `ConfigFile` and `Watch` constructor arguments in the line you added to `AssemblyInfo.cs`. For example: `[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.Config", Watch = true)]`. – hunch_hunch Nov 17 '14 at 15:35
  • Thanks for it's still not working –  Nov 17 '14 at 15:56
  • 5
    Is the .config still residing next to your exe when executing the exe? In other words haven't you copied the exe to a new location before executing without also copying the config file? – Björn Boxstart Nov 17 '14 at 15:56
  • That was the problem sorry ! Thanks a lot –  Nov 17 '14 at 16:07
  • this helped me too. Nice catch. I missed that too. – NewTech Nov 04 '16 at 19:58

1 Answers1

0

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