开发者

Have a Log4Net RollingFileAppender set to roll weekly

开发者 https://www.devze.com 2023-01-16 12:00 出处:网络
The DatePattern string needs to be something that the SimpleDateFormatter will a开发者_C百科ccept.

The DatePattern string needs to be something that the SimpleDateFormatter will a开发者_C百科ccept.

Unfortunately this means that, out of the box, this doesn't include being able to set the boundary to be a week number. There are ways of getting this value in C#, but it's not obvious that we can extend the SimpleDateFormatter or provide a different implementation of IDateFormatter and use that instead (or even in a custom RollingFileAppender).

So how might we get a Log4Net RollingFileAppender to roll weekly?


I got mine rolling every week. You must set your dateformat to include the day of the month to generate unique filenames.

class RollingOverWeekFileAppender : RollingFileAppender
{
    private DateTime nextWeekendDate;

    public RollingOverWeekFileAppender()
    {
        CalcNextWeekend(DateTime.Now);
    }

    private void CalcNextWeekend(DateTime time)
    { 
        // Calc next sunday
        time = time.AddMilliseconds((double)-time.Millisecond);
        time = time.AddSeconds((double)-time.Second);
        time = time.AddMinutes((double)-time.Minute);
        time = time.AddHours((double)-time.Hour);
        nextWeekendDate = time.AddDays((double)(7 - (int)time.DayOfWeek));
    }

    protected override void AdjustFileBeforeAppend()
    {
        DateTime now = DateTime.Now;

        if (now >= nextWeekendDate)
        {
            CalcNextWeekend(now);
            // As you included the day and month AdjustFileBeforeAppend takes care of creating 
            // new file with the new name
            base.AdjustFileBeforeAppend();
        }
    }
}


It is not that simple. The RollingFileAppender uses DateTime.ToString() to determine the "roll point". The statement of the log4net help ist not wrong since the SimpleDateFormatter uses this method as well but it is somewhat misleading: You cannot inject a different date formatter to make the rolling file appender work the way you want.

If you really need roll by week feature then the easiest way would be to derive from the RollingFileAppender and override the AdjustFileBeforeAppend() method. Did not test this, but that should do the trick.


I also confront the same issue to roll the logging file weekly, by testing the method of GLM's solution, somehow it is not working on the version of log4net (1.2.15.0), but inspired by his answer, and study the source code, I have made a solution to generate the log file by name per week (named as Sunday's date)

namespace log4net.Appender
{
    class RollingOverWeekFileAppender : RollingFileAppender
    {
        public RollingOverWeekFileAppender()
        {
            IDateTime dt = new SundayDateTime();
            DateTimeStrategy = dt;
        }

        class SundayDateTime : IDateTime
        {
            public DateTime Now
            {
                get { return CalcThisSunday(DateTime.Now); }
            }

            private DateTime CalcThisSunday(DateTime time)
            {
                // Calc this sunday
                time = time.AddMilliseconds((double)-time.Millisecond);
                time = time.AddSeconds((double)-time.Second);
                time = time.AddMinutes((double)-time.Minute);
                time = time.AddHours((double)-time.Hour);
                return time.AddDays((double)(-(int)time.DayOfWeek));
            }
        }
    }
}

And my snippet of the log.config

<appender name="Log" type="log4net.Appender.RollingOverWeekFileAppender">
  <file type="log4net.Util.PatternString">
    <conversionPattern value=".\log-" />
  </file>
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  <encoding value="utf-8" />
  <staticLogFileName value="false"/>
  <appendToFile value="true" />
  <rollingStyle value="Date"/>
  <datePattern value="yyyyMMdd.lo\g"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date&#9;%level&#9;%logger{1}&#9;%message%newline" />
  </layout>
</appender>
0

精彩评论

暂无评论...
验证码 换一张
取 消