将 Log4Net RollingFileAppender 设置为每周滚动
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我每周都会滚动。您必须将日期格式设置为包含月份中的日期才能生成唯一的文件名。
I got mine rolling every week. You must set your dateformat to include the day of the month to generate unique filenames.
事情没那么简单。 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 theSimpleDateFormatter
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 theAdjustFileBeforeAppend()
method. Did not test this, but that should do the trick.我也遇到同样的问题,每周滚动日志文件,通过测试GLM解决方案的方法,不知怎的,它不适用于log4net(1.2.15.0)的版本,但受到他的回答的启发,并研究了源代码,我已经制定了一个解决方案,每周按名称生成日志文件(命名为星期日的日期)
以及我的 log.config 片段
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)
And my snippet of the log.config