代码之家  ›  专栏  ›  技术社区  ›  Unsliced

将Log4Net RollingFileAppender设置为每周滚动一次

  •  5
  • Unsliced  · 技术社区  · 14 年前

    这个 DatePattern 字符串必须是 SimpleDateFormatter will accept .

    不幸的是,这意味着,开箱即用,这不包括能够将边界设置为周数。有几种方法可以在C#中获得这个值,但是我们不能很明显地扩展 或者提供不同的实现 IDateFormatter RollingFileAppender ).

    那么我们怎样才能得到一个Log4Net呢 每周一次?

    3 回复  |  直到 8 年前
        1
  •  2
  •   Guillermo Ruffino    13 年前

    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();
            }
        }
    }
    
        2
  •  1
  •   Stefan Egli    14 年前

    不是那么简单。RollingFileAppender使用 DateTime.ToString() SimpleDateFormatter 也使用此方法,但它有点误导:您不能插入其他日期格式化程序,使滚动文件附加器按您想要的方式工作。

    RollingFileAppender AdjustFileBeforeAppend() 方法。没有测试这一点,但这应该可以解决问题。

        3
  •  1
  •   DevHang    8 年前

    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));
                }
            }
        }
    }
    

    我的片段日志.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>