You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Shireesh Thanneru <th...@yahoo.com> on 2006/12/06 07:24:04 UTC

Question on Date/Time based rolling

I need to have date based rolling for log files and want to keep only the 30 log files (corresponding to the last 30 days). Is that possible (with MaxSizeRollBackups or some other way)? If yes, please send me the corresponding xml configuration for the appender. Based on the RollingFileAppender documentation, it says

"CAUTION     A maximum number of backup files when rolling on date/time boundaries is not  supported."
Is that still true? If yes, what are the other ways of achieving this?

Thanks in advance.

Shiressh Thanneru

 
---------------------------------
Everyone is raving about the all-new Yahoo! Mail beta.

Re: Question on Date/Time based rolling

Posted by Karel Kral <kr...@volny.cz>.
Thanks! Your appender saves my work, which I had to do...
___________________________________________________
Karel Kral, vyvojar
ANETE, s.r.o.
Prostredi: VB.NET, VS2005, XP Pro/P4 3GHz, 2GB RAM
___________________________________________________

On 13.12.2006 10:38, Geert Verbakel wrote:
> 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
> 
>     }
> 
> }
> 
>  
> 

AdoNetAppender configuration note

Posted by ha...@pnc.com.
Hello, All.

In case some folks have been wondering why they cannot get the
AdoNetAppender to work, along with maybe also using custom parameter
fields, I found that the log4net documentation and what works for my
projects is different when interfacing with SQL Server 2005.

In the web/app config file in the AdoNetAppender, change the buffersize
from the documented 100 to 1, like so:

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="1" />
      . . .
The default bufferSize is 512, so not sure why the docs set it at 100.  In
any case, setting it to "1" turns off buffering of the logging events and
sends the transmission synchronously.  Another option may be to increase
the buffer size to some number that is big enough to hold your logging
event, but not buffering seems to work.

Harry


The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US Law. PNC�s postal address is 249 Fifth Avenue, Pittsburgh, PA 15222. If you do not wish to receive any additional advertising or promotional messages from PNC at this e-mail address, click here to Unsubscribe. https://pnc.p.delivery.net/m/u/pnc/uni/p.asp By unsubscribing to this message, you will be unsubscribed from all advertising or promotional messages from PNC. Removing your e-mail address from this mailing list will not affect your subscription to alerts, e-newsletters or account servicing e-mails.

Re: Question on Date/Time based rolling

Posted by Geert Verbakel <ge...@gmail.com>.
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

    }

}

Re: Question on Date/Time based rolling

Posted by Daniel Essin <es...@ieee.org>.
OK, I'll give it a try.

Dan

Peter Drier wrote:
> Please feel free to add it and submit a patch.
>
> I thought about adding it a while back, but came to a few challenges.. 
> 1.  With the different ways that a file can be rolled, there are more 
> permutations than I originally thought
> 2.  Most organizations already have a way to deal with rotation of log 
> files. 
> 3.  To anyone worth their salt, log files are a religious item.  Mess 
> them up and be hated forever.
>
> And given we have #2 at my organization, my interest waned.. 
>
> If you are to write one, please make sure it's in the spirit of the 
> existing MaxSizeRollBackups, and takes into account rollovers by both 
> day and file size being exceeded/rolled. 
>
> Cheers,
> Peter
>
> On 12/6/06, *Daniel Essin* <essin@ieee.org <ma...@ieee.org>> 
> wrote:
>
>     This sound like core functionality, It should probably be be
>     included.
>
>     Dan
>
>     Peter Drier wrote:
>>     why not use any type of standard log file cleaner external to
>>     your log4net application.. 
>>
>>     Or write a routine at startup that checks for files older than
>>     xxx days in a specific (log) directory and deletes them. 
>>
>>     -Peter
>>
>>     On 12/6/06, *Shireesh Thanneru* <thanneru@yahoo.com
>>     <ma...@yahoo.com>> wrote:
>>
>>         I need to have date based rolling for log files and want to
>>         keep only the 30 log files (corresponding to the last 30
>>         days). Is that possible (with aMaxSizeRollBackups or some
>>         other wy)? If yes, please send me the corresponding xml
>>         configuration for the appender. Based on the
>>         RollingFileAppender documentation, it says
>>
>>         "*CAUTION*   
>>         A maximum number of backup files when rolling on date/time
>>         boundaries is not supported."
>>         Is that still true? If yes, what are the other ways of
>>         achieving this?
>>
>>         Thanks in advance.
>>
>>         Shiressh Thanneru
>>
>>         ------------------------------------------------------------------------
>>         Everyone is raving about the all-new Yahoo! Mail beta.
>>         <http://us.rd.yahoo.com/evt=45083/*http://advision.webevents.yahoo.com/mailbeta>
>>
>>
>

Re: Question on Date/Time based rolling

Posted by Peter Drier <pe...@gmail.com>.
Please feel free to add it and submit a patch.

I thought about adding it a while back, but came to a few challenges..
1.  With the different ways that a file can be rolled, there are more
permutations than I originally thought
2.  Most organizations already have a way to deal with rotation of log
files.
3.  To anyone worth their salt, log files are a religious item.  Mess them
up and be hated forever.

And given we have #2 at my organization, my interest waned..

If you are to write one, please make sure it's in the spirit of the existing
MaxSizeRollBackups, and takes into account rollovers by both day and file
size being exceeded/rolled.

Cheers,
Peter

On 12/6/06, Daniel Essin <es...@ieee.org> wrote:
>
>  This sound like core functionality, It should probably be be included.
>
> Dan
>
> Peter Drier wrote:
>
> why not use any type of standard log file cleaner external to your log4net
> application..
>
> Or write a routine at startup that checks for files older than xxx days in
> a specific (log) directory and deletes them.
>
> -Peter
>
> On 12/6/06, Shireesh Thanneru <th...@yahoo.com> wrote:
> >
> > I need to have date based rolling for log files and want to keep only
> > the 30 log files (corresponding to the last 30 days). Is that possible (with
> > aMaxSizeRollBackups or some other wy)? If yes, please send me the
> > corresponding xml configuration for the appender. Based on the
> > RollingFileAppender documentation, it says
> >
> > "*CAUTION*    A maximum number of backup files when rolling on date/time
> > boundaries is not supported."
> > Is that still true? If yes, what are the other ways of achieving this?
> >
> > Thanks in advance.
> >
> > Shiressh Thanneru
> >
> >  ------------------------------
> > Everyone is raving about the all-new Yahoo! Mail beta.
> > <http://us.rd.yahoo.com/evt=45083/*http://advision.webevents.yahoo.com/mailbeta>
>
>
>

Re: Question on Date/Time based rolling

Posted by Daniel Essin <es...@ieee.org>.
This sound like core functionality, It should probably be be included.

Dan

Peter Drier wrote:
> why not use any type of standard log file cleaner external to your 
> log4net application.. 
>
> Or write a routine at startup that checks for files older than xxx 
> days in a specific (log) directory and deletes them. 
>
> -Peter
>
> On 12/6/06, *Shireesh Thanneru* <thanneru@yahoo.com 
> <ma...@yahoo.com>> wrote:
>
>     I need to have date based rolling for log files and want to keep
>     only the 30 log files (corresponding to the last 30 days). Is that
>     possible (with MaxSizeRollBackups or some other way)? If yes,
>     please send me the corresponding xml configuration for the
>     appender. Based on the RollingFileAppender documentation, it says
>
>     "*CAUTION*   
>     A maximum number of backup files when rolling on date/time
>     boundaries is not supported."
>     Is that still true? If yes, what are the other ways of achieving this?
>
>     Thanks in advance.
>
>     Shiressh Thanneru
>
>     ------------------------------------------------------------------------
>     Everyone is raving about the all-new Yahoo! Mail beta.
>     <http://us.rd.yahoo.com/evt=45083/*http://advision.webevents.yahoo.com/mailbeta>
>
>

Re: Question on Date/Time based rolling

Posted by Peter Drier <pe...@gmail.com>.
why not use any type of standard log file cleaner external to your log4net
application..

Or write a routine at startup that checks for files older than xxx days in a
specific (log) directory and deletes them.

-Peter

On 12/6/06, Shireesh Thanneru <th...@yahoo.com> wrote:
>
> I need to have date based rolling for log files and want to keep only the
> 30 log files (corresponding to the last 30 days). Is that possible (with
> MaxSizeRollBackups or some other way)? If yes, please send me the
> corresponding xml configuration for the appender. Based on the
> RollingFileAppender documentation, it says
>
> "*CAUTION*    A maximum number of backup files when rolling on date/time
> boundaries is not supported."
> Is that still true? If yes, what are the other ways of achieving this?
>
> Thanks in advance.
>
> Shiressh Thanneru
>
> ------------------------------
> Everyone is raving about the all-new Yahoo! Mail beta.<http://us.rd.yahoo.com/evt=45083/*http://advision.webevents.yahoo.com/mailbeta>
>
>