Alex Rivera | Logout

Writing logs to file

Asked 2012-03-21T13:09:16.890
40

I have some troubles while writing logs from log4net to the file. I seem to do all as described in manual, but that does not work. Here is my logging.config file:

<?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">
      <file value="log.txt" />
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %message%newline" />
      </layout>
    </appender>
  </log4net>
</configuration>

Please help with an example of configuration file which actually works.

Edit
Report

1 Answer

6

There are two things that you can do:

One, if you want to use a seperate config file, by adding the following to you app.config file it will configure logging automatically.

<?xml version="1.0"?>
<configuration>
    <appSettings>
        <add key="log4net.Config" value="log4.config"/>
        <add key="log4net.Config.Watch" value="True"/>
        <add key="log4net.Internal.Debug" value="False"/>
    </appSettings>
</configuration>

Otherwise, you need to initiate logging in the start of your application.

//Initiate logging based on web.config file
log4net.Config.XmlConfigurator.Configure();

// Create a logger for use in this class
log4net.ILog log4 = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
answered 2012-03-21T13:16:31.053

Your Answer