Have a Log4Net RollingFileAppender set to roll weekly

前端 未结 3 1843
感动是毒
感动是毒 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: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

    
      
        
      
      
      
      
      
      
      
      
        
      
    
    

提交回复
热议问题