Log4Net可以自动删除日志文件吗? [重复]

时间:2021-07-08 03:42:02

This question already has an answer here:

这个问题在这里已有答案:

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:

我在用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>

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?

我只想保留30天的日志。如何配置log4net自动删除日志?如果没有通过log4net可用,我会有什么解决方案?

Thank you in advance.

先谢谢你。

3 个解决方案

#1


9  

Here is another question from here on SO that asks essentially the same thing: Log4Net: set Max backup files on RollingFileAppender with rolling Date

这里是另一个问题,从这里问到基本相同的事情:Log4Net:使用滚动日期在RollingFileAppender上设置最大备份文件

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.

共识是log4net不直接支持它。一张海报说它已经在log4net的源代码库中实现,但还没有发布。另一张海报显示了他用来定期清理旧文件的一些代码。

#2


4  

I'm pretty sure that you can't do with the existing appender, although I can't confirm it.

我很确定你不能使用现有的appender,虽然我无法确认。

However, I see two options:

但是,我看到两个选择:

  1. Create your own appender, subclassing RollingFileAppender (or, if you want more control, subclass FileAppender). Then change your config file to use that appender (change the appender element).
  2. 创建自己的appender,子类化RollingFileAppender(或者,如果你想要更多的控件,可以创建子类FileAppender)。然后更改配置文件以使用该appender(更改appender元素)。
  3. Create a .bat file that deletes files older than x days (see: Batch file to delete files older than N days). Then create a task in Windows (http://support.microsoft.com/kb/308569) that runs this bat file e.g. every day.
  4. 创建一个删除超过x天的文件的.bat文件(请参阅:批处理文件以删除超过N天的文件)。然后在Windows(http://support.microsoft.com/kb/308569)中创建一个运行此bat文件的任务,例如每天。

#3


-1  

This should provide some help. Look for the RollingFileAppender section

这应该提供一些帮助。查找RollingFileAppender部分

#1


9  

Here is another question from here on SO that asks essentially the same thing: Log4Net: set Max backup files on RollingFileAppender with rolling Date

这里是另一个问题,从这里问到基本相同的事情:Log4Net:使用滚动日期在RollingFileAppender上设置最大备份文件

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.

共识是log4net不直接支持它。一张海报说它已经在log4net的源代码库中实现,但还没有发布。另一张海报显示了他用来定期清理旧文件的一些代码。

#2


4  

I'm pretty sure that you can't do with the existing appender, although I can't confirm it.

我很确定你不能使用现有的appender,虽然我无法确认。

However, I see two options:

但是,我看到两个选择:

  1. Create your own appender, subclassing RollingFileAppender (or, if you want more control, subclass FileAppender). Then change your config file to use that appender (change the appender element).
  2. 创建自己的appender,子类化RollingFileAppender(或者,如果你想要更多的控件,可以创建子类FileAppender)。然后更改配置文件以使用该appender(更改appender元素)。
  3. Create a .bat file that deletes files older than x days (see: Batch file to delete files older than N days). Then create a task in Windows (http://support.microsoft.com/kb/308569) that runs this bat file e.g. every day.
  4. 创建一个删除超过x天的文件的.bat文件(请参阅:批处理文件以删除超过N天的文件)。然后在Windows(http://support.microsoft.com/kb/308569)中创建一个运行此bat文件的任务,例如每天。

#3


-1  

This should provide some help. Look for the RollingFileAppender section

这应该提供一些帮助。查找RollingFileAppender部分