We had the same requirement and created our own derived appender.
It has some limitations (restriction on the date format), but seems to be
working fine for the rest.

Please give your comments if you think something could be ameliorated...

Regards,
Geert



#region imports



using System;

using System.IO;

using log4net.Core;



#endregion



namespace Utils.Logging

{

   #region summary & change history



   /// <summary>

   /// Updated RollingFileAppender that removes log files that are older
than a defined number of days.

   /// </summary>

   /// <remarks>

   /// Only works with date pattern that starts with yy or .yy

   /// </remarks>



   #endregion



   public class RollingFileAppender : log4net.Appender.RollingFileAppender

   {

       #region private fields



       private DateTime lastDeleteTime = DateTime.MinValue;

       private bool doCheck = false;

       string fileName = null;



       #endregion



       #region constructor



       /// <summary>

       /// Initializes a new instance of the <see
cref="RollingFileAppender"/> class.

       /// </summary>

       public RollingFileAppender()

       {

       }



       #endregion



       #region methods



       /// <summary>

       /// Activates the options.

       /// </summary>

       public override void ActivateOptions()

       {

           base.ActivateOptions ();



           fileName = null;

           if (DatePattern.StartsWith("yy"))

           {

               fileName = File + DateTime.Now.ToString("yyyy").Substring(0,
1);

           }

           else if (DatePattern.StartsWith(@"\.yy"))

           {

               fileName = File + DateTime.Now.ToString(".yyyy").Substring(0,
2);

           }



           doCheck = fileName != null

               && MaxSizeRollBackups > 0

               && (RollingStyle == RollingMode.Date

               || RollingStyle == RollingMode.Composite);

       }





       /// <summary>

       /// Handles append time behaviour for CompositeRollingAppender.  This
checks

       /// if a roll over either by date (checked first) or time (checked
second)

       /// is need and then appends to the file last.

       /// </summary>

       /// <param name="loggingEvent"></param>

       override protected void Append(LoggingEvent loggingEvent)

       {

           base.Append(loggingEvent);



           DeleteTime();

       }



       /// <summary>

       /// Deletes the rolling time files older than a defined number of
days.

       /// </summary>

       private void DeleteTime()

       {

           if (doCheck &&

               DateTime.Now.Subtract(lastDeleteTime).TotalHours >= 1)

           {

               lastDeleteTime = DateTime.Now;



               try

               {

                   string[] files = Directory.GetFiles(
Path.GetDirectoryName(File));

                    foreach (string file in files)

                    {

                        if (file.StartsWith(fileName))

                        {

                            FileInfo info = new FileInfo(file);

                            if
(DateTime.Now.Subtract(info.LastWriteTime).TotalDays
MaxSizeRollBackups)

                            {

                                DeleteFile(file);

                            }

                        }

                    }

               }

               catch (Exception ex)

               {

                    ErrorHandler.Error("Exception while deleting the
rolling time files older than a defined number of days.", ex);

               }

           }

       }



       #endregion

   }

}

Reply via email to