Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring log4net with xml file

I tried to configure log4net for logging everything to the console output. I have a config file named Log4Net.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="INFO" />
      <appender-ref ref="ConsoleAppender" />
    </root>
  </log4net>
</configuration>

and I have my main class (just a testing case)

namespace TestLog4Net {
    class Program {
        private static readonly ILog log = LogManager.GetLogger(typeof(Program));

        static void Main(string[] args) {
            log.Info("Info");
            log.Error("Error");
            Console.ReadKey();
        }
    }
}

I added these lines to the AssemblyInfo.cs

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

But now nothing is logged, can someone explain this?

like image 259
Sebastian Müller Avatar asked Aug 24 '09 09:08

Sebastian Müller


People also ask

Where is log4net configuration file?

You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation.

What is log4net XML?

It's probably an XML documentation file, which contains the XML comments on all of the members in the log4net project. It's not at all necessary for installation, so you can delete it if you want. You can also tell the compiler not to emit XML documentation for a project in that project's properties.


1 Answers

When you have log4net config in a separate config file you should not include the configuration and configSections elements. log4net should be the top level element after the xml declartion.

like image 119
Peter Lillevold Avatar answered Oct 05 '22 01:10

Peter Lillevold