Log4Net可以自动删除日志文件吗?
我在用 C# 编写的 Windows 服务程序中使用 log4net RollingFileAppender。日志目录中的文件数量和大小增长太快,需要清理。配置如下:
<appender name="Rolling Log" type="log4net.Appender.RollingFileAppender">
<file value="..\logs\MyProgram.%cs{instanceName}.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value=".yyyy-MM-dd.lo\g" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="150MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5level] %property{remoteUser} [%threadIdentity] %type{1}.%method - %message%newline%exception" />
</layout>
我只想保留 30 天的日志。如何配置 log4net 自动删除日志?如果无法通过 log4net 获得,我会有什么解决方案?
先感谢您。
I am using log4net RollingFileAppender in a windows service program written in C#. The number and size of files in logs directory is growing too fast, need cleanup. The configuration is below:
<appender name="Rolling Log" type="log4net.Appender.RollingFileAppender">
<file value="..\logs\MyProgram.%cs{instanceName}.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value=".yyyy-MM-dd.lo\g" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="150MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5level] %property{remoteUser} [%threadIdentity] %type{1}.%method - %message%newline%exception" />
</layout>
I only want to keep 30 days of logs. How can I configure log4net to delete the logs automatically? If not available through log4net, what solutions would I have?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是这里的另一个问题,本质上问的是同样的事情:
Log4Net:使用滚动日期设置 RollingFileAppender 上的最大备份文件< /a>
共识是 log4net 不直接支持它。一位发帖者表示,它已在 log4net 的源代码存储库中实现,但尚未发布。另一张海报展示了他用来定期清理旧文件的一些代码。
Here is another question from here on SO that asks essentially the same thing:
Log4Net: set Max backup files on RollingFileAppender with rolling Date
The consensus was that log4net does not support it directly. One poster says that it has been implemented in log4net's source code repository, but has not been released yet. Another poster shows some code that he uses to cleanup old files periodically.
我很确定您无法使用现有的附加程序,尽管我无法确认这一点。
但是,我看到两个选项:
I'm pretty sure that you can't do with the existing appender, although I can't confirm it.
However, I see two options:
这应该提供一些帮助。查找 RollingFileAppender 部分
This should provide some help. Look for the RollingFileAppender section