Tony Vitabile
Tony Vitabile

Reputation: 8594

Log4net stopped writing to the event log

My application uses Log4net to write to the event viewer. Here's the log4net section of my program's App.config file:

<log4net>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
        <applicationName value="CarSystem" />
        <logName value="CarSystemLog" />
        <threshold value="DEBUG" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
        </layout>
    </appender>
    <root>
        <level value="DEBUG" />
        <appender-ref ref="EventLogAppender" />
    </root>
</log4net> 

Now, these settings were working fine up until about 12:30 pm the day before yesterday. Suddenly, (just when I need to review the messages in the log, of course) it stopped writing to the log file.

The custom event log is in the viewer. I've rebooted the machine today, but still nothing new is going into the log. I increased the maximum log size to 10880 KB.

Why isn't log4net writing to my log file any more?

Upvotes: 2

Views: 4457

Answers (2)

Marcel N.
Marcel N.

Reputation: 13976

Rather than looking for a solution, better find the cause.

I suggest you enable log4net's internal debug logging to file (any file) and it will dump it's troubles there. I've used this many times when my log files (should be same thing for Event Log) didn't show up.

Instructions on how to configure it here.

Quick steps:

  • Add this to your app.config:
 <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
     <appSettings>
         <add key="log4net.Internal.Debug" value="true"/>
     </appSettings>
 </configuration>
  • Add this too to the config (configuration section).
 <system.diagnostics>
    <trace autoflush="true">
        <listeners>
              <add 
              name="textWriterTraceListener" 
              type="System.Diagnostics.TextWriterTraceListener" 
              initializeData="C:\tmp\log4net.txt" />
        </listeners>
    </trace>
 </system.diagnostics>

Any issues with permissions or whatever will then be logged to the trace file.

Upvotes: 11

Tony Vitabile
Tony Vitabile

Reputation: 8594

I went back to the Log4Net documentation and noticed this statement:

it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked.

Well, I had the XmlConfiguration attribute in my AssemblyInfo.cs and a call to LogManager.GetLogger in the code, but it was after a call to start a monitoring object I had added to the constructor of my WPF applicaton's App object. I realized that the logging stopped after I had added this call when I read this statement and reviewed the code.

So I moved the call to LogManager.GetLogger to be the very first statement in the App constructor and the logging began again. Hallelieuia!

Thanks for the help and suggestions.

Upvotes: 2

Related Questions