您是否知道 log4net 附加程序可以按日期滚动,但可以限制文件总数?

发布于 2024-08-17 15:36:09 字数 285 浏览 11 评论 0原文

我需要为 log4net 定义一个附加程序,每天获取一个日志文件,但文件总数限制为 30 个。也就是说,我只想保留不超过 30 天的日志,删除旧的。

我尝试过使用 RollingFileAppender 来完成此操作,但似乎 不支持指定要保留的文件限制。您知道我可以使用的替代解决方案吗?

I need to define an appender for log4net in a way that I get one log file for each day, but the total number of files are limited to, let's say, 30. That is I want to keep only the logs not older then 30 days, delete the older ones.

I've tried doing it with RollingFileAppender, but it seems that specifying a limit of files to keep is not supported. Do you know of an alternative solution that I could use?

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

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

发布评论

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

评论(3

So要识趣 2024-08-24 15:36:09

几个月前我花了一些时间研究这个问题。 v1.2.10 不支持根据日期滚动删除较旧的日志文件。它在下一个版本的任务列表中。我自己获取了源代码并添加了功能,如果其他人感兴趣的话,可以将其发布给他们。该问题和补丁可以在 https://issues.apache.org/jira 找到/browse/LOG4NET-27

I spent some time looking into this a few months ago. v1.2.10 doesn't support deleting older log files based on rolling by date. It is on the task list for the next release. I took the source code and added the functionality myself, and posted it for others if they are interested. The issue and the patch can be found at https://issues.apache.org/jira/browse/LOG4NET-27 .

疾风者 2024-08-24 15:36:09

编写一个 C# WinService 或在您的应用程序中创建一个 BackgroundWorker,它将每天监视一次您的旧日志文件并将其删除。您可以为此范围使用 FileSystemWatcher

或者只是测试每个运行的应用程序是否存在新日志文件并将其删除。

另一种选择是不在本地存储文件,而是通过网络(邮件、远程服务器等)发送文件。

另请参阅重复的问题:此处此处

Write a C# WinService or make in your application a BackgroundWorker that will monitor once a day your old log files and delete it. You can use the FileSystemWatcher for this scope.

Or just test at each application running the presence of new log files and delete it.

An other option will be do not store files locally but send it via network (mail, distant server etc).

See also duplicate questions: here and here.

倚栏听风 2024-08-24 15:36:09

您可以使用“复合”日志类型吗?更多信息: http://logging.apache.org/log4net/release/config -examples.html

 <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logfile" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>

can you use the 'Composite' log type? more here: http://logging.apache.org/log4net/release/config-examples.html

 <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logfile" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文