1

I have one windows service. For Logging purpose in that service I am using Log4Net.dll. Now my requirement is that I want to create a new log file everyday and it should keep the log of only previous 7 days. Means on 8th day, it should delete first day file and use new file. I am using appender as:

 <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="D:\Log\%property{LogName}" />
     <AppendToFile value="true" />
     <rollingStyle value="Date" />      
     <maxSizeRollBackups value="100" />       
     <staticLogFileName value="false" />
     <layout type="log4net.Layout.PatternLayout">
       <conversionPattern value="%newline %date %-5level %C.%M() - %message" />
     </layout>
 </appender>

And at service startup I am configuring logger as:

log4net.GlobalContext.Properties["LogName"] = "App_" + DateTime.Now.ToString("MMddyyyy") + ".log";
log4net.Config.XmlConfigurator.Configure();   

So is there any functionality of Log4Net that will achieve above functionality? Or is there any other way to do it? Any type of help will be appreciated.

Nicktar
  • 5,548
  • 1
  • 28
  • 43
Dany
  • 2,034
  • 8
  • 34
  • 54

1 Answers1

1

I'm afraid you can't: RollingFileAppender Class

A maximum number of backup files when rolling on date/time boundaries is not supported

Have a look at this thread for other suggestions: Log4Net: set Max backup files on RollingFileAppender with rolling Date

To implement log file everyday you would use:

<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
Community
  • 1
  • 1