How can you delete old log4net logfiles?
We have an application that logs using log4net. But we would like to delete the logfiles every 4 weeks (automatically).
Is there an option in log4net to do this or do we need to have a work arround?Cheers, M.
this is my configuration
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppen开发者_Go百科der">
<file value="Logs/Log4Net/"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<staticLogFileName value="false" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<datePattern value="yyyy-MM-dd-HH.lo\g" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
</root>
If this answer doesn't help you, then you can also come up with a windows service that would do the purge-job.
I would suggest you using the windows task scheduler for this job.
You could do this in the program:
string[] logFiles = Directory.GetFiles("Logs\\");
foreach (string logFile in logFiles)
{
FileInfo fileInfo = new FileInfo(logFile);
if (fileInfo.CreationTime < DateTime.Now.AddMonths(-1))
{
fileInfo.Delete();
}
}
精彩评论