C# – log4net log files disappear when service restarted

clog4netloggingnet

We are using log4net to create our logfiles from Windows services, and we are using the RollingFileAppender rolling based on date. The version of log4net we are using is 1.2.9. Now for the issue. We are rolling based on date, and on the days we need to restart a service the log file for that day is not rolled.

Example: Say today is November 16th. I have logfile.txt that contains today's information, and I have logfile.txt.20091115, logfile.txt.20091112, and logfile.txt.20091111. I am missing the files from 11/13 and 11/14 because the service was restarted on both of those days.

As anyone else experienced this or know why this is happening?

Update:

Here is my log4net.config appender section

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <param name="File" value="logfile.txt" />
  <param name="AppendToFile" value="true" />
  <param name="MaxSizeRollBackups" value="10" />
  <param name="MaximumFileSize" value="1000KB" />
  <param name="RollingStyle" value="Date" />
  <param name="DatePattern" value="yyyyMMdd" />
  <param name="StaticLogFileName" value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <param name="Header" value="[Service Started]&#13;&#10;" />
    <param name="Footer" value="[Service Stopped]&#13;&#10;" />
    <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
  </layout>
</appender>

As you can see the AppendToFile param is set to true.

I want to clarify something. The file does not get overwritten at the time I restart the service. When the file is suppose to roll based on date, is when the file disappears.

Best Answer

As empi says, we'll need to see your config file to tell for sure. However, I'll bet you have the Append property set to false. From the log4net docs:

If the value is set to false then the file will be overwritten, if it is set to true then the file will be appended to.

Try adding this to your RollingFileAppender config:

 <appendToFile value="true" />

EDIT: Looking at your posted config file, this line looks odd:

<param name="StaticLogFileName" value="true" />

...which is documented as:

Gets or sets a value indicating whether to always log to the same file. true if always should be logged to the same file, otherwise false.

That sounds like it matches what you're seeing. Try removing that line, or setting it to false.