Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4Net creating empty log files, not actually logging

Tags:

c#

log4net

I've got an issue with my Log4Net usage in my application. This is my app.config file...

    <configuration>
  <log4net>
    <root>
      <level value="INFO"/>
      <appender-ref ref="FileAppender" />
    </root>
      <appender name="FileAppender" type="log4net.Appender.FileAppender">
        <file value="mylogfile.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>

  </log4net>


  <configSections>
    <section name="log4net"
             type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" 
         />
  rest of app.config under here...

This is at the top of my Program.cs

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

I also have this at the top of my form partial class (is this a duplicate of the above?)

 private static readonly log4net.ILog log = log4net.LogManager.GetLogger
                (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

And this code is on the form load (which does nothing)

log.Info("Initializing...");
            log.Fatal("blah");

The logger creates a log file called "mylogfile.txt" but doesn't actually write anything into it.

I know I'm doing something wrong, but I can't spot what it is easily :( any help would be awesome.

like image 563
Lewis Cianci Avatar asked Jul 22 '15 23:07

Lewis Cianci


People also ask

What is rolling file Appender in log4net?

RollingFileAppender can roll log files based on size or date or both depending on the setting of the RollingStyle property. When set to Size the log file will be rolled once its size exceeds the MaximumFileSize.

Where are log4net logs stored?

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

Is log4net a Threadsafe?

Is log4net thread-safe? Yes, log4net is thread-safe.


1 Answers

I wound up figuring it out myself seconds later. The app.config was wrong (unsurprisingly, considering I wrote it).

Here is the working app.config startup.

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

             />
   <--- other section groups here, you will have these too --->
    </sectionGroup>
  </configSections>
  <log4net>
    <root>
      <level value="INFO"/>
      <appender-ref ref="FileAppender" />
    </root>
      <appender name="FileAppender" type="log4net.Appender.FileAppender">
        <file value="mylogfile.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>

  </log4net>

I canned the

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

and replaced it with log4net.Config.XmlConfigurator.Configure(); (here it is, where it is called in the Main program startup in Program.cs).

    static void Main()
    {
        log4net.Config.XmlConfigurator.Configure();

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new frmMOEBackup());
    }

And it works! Thanks to everyone who viewed my question :)

like image 181
Lewis Cianci Avatar answered Oct 04 '22 21:10

Lewis Cianci