Have a Log4Net RollingFileAppender set to roll weekly

我的未来我决定 提交于 2019-11-28 12:31:40

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!