Have a Log4Net RollingFileAppender set to roll weekly

前端 未结 3 1828
感动是毒
感动是毒 2020-12-11 07:23

The DatePattern string needs to be something that the SimpleDateFormatter will accept.

Unfortunately this means that, out of the box, this

相关标签:
3条回答
  • 2020-12-11 07:36

    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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-11 07:40

    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.

    0 讨论(0)
  • 2020-12-11 07:56

    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 讨论(0)
提交回复
热议问题