将 Log4Net RollingFileAppender 设置为每周滚动

发布于 2024-09-19 16:42:46 字数 466 浏览 6 评论 0原文

DatePattern 字符串需要是 SimpleDateFormatter 将接受

不幸的是,这意味着,开箱即用,这不包括能够将边界设置为周数。有多种方法可以在 C# 中获取此值,但我们是否可以扩展 SimpleDateFormatter 或提供 IDateFormatter 的不同实现并使用它(甚至在自定义 RollingFileAppender)。

那么我们如何让 Log4Net RollingFileAppender 每周滚动一次呢?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

来日方长 2024-09-26 16:42:46

我每周都会滚动。您必须将日期格式设置为包含月份中的日期才能生成唯一的文件名。

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

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();
        }
    }
}
汹涌人海 2024-09-26 16:42:46

事情没那么简单。 RollingFileAppender 使用 DateTime.ToString() 来确定“滚动点”。 log4net 帮助的声明没有错误,因为 SimpleDateFormatter 也使用此方法,但它有些误导:您不能注入不同的日期格式化程序来使滚动文件附加程序按照您想要的方式工作。

如果您确实需要按周滚动功能,那么最简单的方法是从 RollingFileAppender 派生并重写 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.

日记撕了你也走了 2024-09-26 16:42:46

我也遇到同样的问题,每周滚动日志文件,通过测试GLM解决方案的方法,不知怎的,它不适用于log4net(1.2.15.0)的版本,但受到他的回答的启发,并研究了源代码,我已经制定了一个解决方案,每周按名称生成日志文件(命名为星期日的日期)

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

以及我的 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	%level	%logger{1}	%message%newline" />
  </layout>
</appender>

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	%level	%logger{1}	%message%newline" />
  </layout>
</appender>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文