Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Even though its not supported, here is how I handled this situation:</p> <p>This is my configuration:</p> <pre><code> &lt;appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"&gt; &lt;file value="C:\logs\LoggingTest\logfile.txt" /&gt; &lt;appendToFile value="true" /&gt; &lt;rollingStyle value="Composite" /&gt; &lt;datePattern value="yyyyMMdd" /&gt; &lt;maxSizeRollBackups value="10" /&gt; &lt;maximumFileSize value="1MB" /&gt; &lt;layout type="log4net.Layout.PatternLayout"&gt; &lt;conversionPattern value="%date - %message%newline" /&gt; &lt;/layout&gt; &lt;/appender&gt; </code></pre> <p>On application start up I do:</p> <pre><code> XmlConfigurator.Configure(); var date = DateTime.Now.AddDays(-10); var task = new LogFileCleanupTask(); task.CleanUp(date); </code></pre> <hr> <pre><code>using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using log4net; using log4net.Appender; using log4net.Config; public class LogFileCleanupTask { #region - Constructor - public LogFileCleanupTask() { } #endregion #region - Methods - /// &lt;summary&gt; /// Cleans up. Auto configures the cleanup based on the log4net configuration /// &lt;/summary&gt; /// &lt;param name="date"&gt;Anything prior will not be kept.&lt;/param&gt; public void CleanUp(DateTime date) { string directory = string.Empty; string filePrefix = string.Empty; var repo = LogManager.GetAllRepositories().FirstOrDefault(); ; if (repo == null) throw new NotSupportedException("Log4Net has not been configured yet."); var app = repo.GetAppenders().Where(x =&gt; x.GetType() == typeof(RollingFileAppender)).FirstOrDefault(); if (app != null) { var appender = app as RollingFileAppender; directory = Path.GetDirectoryName(appender.File); filePrefix = Path.GetFileName(appender.File); CleanUp(directory, filePrefix, date); } } /// &lt;summary&gt; /// Cleans up. /// &lt;/summary&gt; /// &lt;param name="logDirectory"&gt;The log directory.&lt;/param&gt; /// &lt;param name="logPrefix"&gt;The log prefix. Example: logfile dont include the file extension.&lt;/param&gt; /// &lt;param name="date"&gt;Anything prior will not be kept.&lt;/param&gt; public void CleanUp(string logDirectory, string logPrefix, DateTime date) { if (string.IsNullOrEmpty(logDirectory)) throw new ArgumentException("logDirectory is missing"); if (string.IsNullOrEmpty(logPrefix)) throw new ArgumentException("logPrefix is missing"); var dirInfo = new DirectoryInfo(logDirectory); if (!dirInfo.Exists) return; var fileInfos = dirInfo.GetFiles("{0}*.*".Sub(logPrefix)); if (fileInfos.Length == 0) return; foreach (var info in fileInfos) { if (info.CreationTime &lt; date) { info.Delete(); } } } #endregion } </code></pre> <p>The Sub Method is an Extension Method, it basically wraps string.format like so:</p> <pre><code>/// &lt;summary&gt; /// Extension helper methods for strings /// &lt;/summary&gt; [DebuggerStepThrough, DebuggerNonUserCode] public static class StringExtensions { /// &lt;summary&gt; /// Formats a string using the &lt;paramref name="format"/&gt; and &lt;paramref name="args"/&gt;. /// &lt;/summary&gt; /// &lt;param name="format"&gt;The format.&lt;/param&gt; /// &lt;param name="args"&gt;The args.&lt;/param&gt; /// &lt;returns&gt;A string with the format placeholders replaced by the args.&lt;/returns&gt; public static string Sub(this string format, params object[] args) { return string.Format(format, args); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload