如何使用Log4Net对日志文件实施自动归档 [英] How to implement auto archiving for log file using Log4Net

查看:385
本文介绍了如何使用Log4Net对日志文件实施自动归档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要配置log4net,这样所有前一天的日志都应自动存档.是否有可能在Log4Net中自动存档以前的日志.我只想使用配置来执行此操作,而无需使用任何第三方库(例如Sharplibzip)编写任何代码来创建存档.

I want to configure log4net in such a manner that all the previous day logs should be archived automatically. Is there any possibility to auto archive previous logs in Log4Net. I want to do this using the configuration only without writing any code using any third party library like sharplibzip to create archive.

通过归档还要添加的另一件事是指以zip/rar格式压缩文件以节省磁盘空间.

One more thing to add by archiving i mean compressing file in zip/rar format to save disk space.

推荐答案

抱歉,如果不编写代码就无法归档文件.但是代码不会很复杂.

It is not possible to archive files without writing code, sorry. However the code would not be very complicated.

您可以创建一个继承自RollingFileAppender的自定义追加程序,并覆盖AdjustFileBeforeAppend方法,以便为文件滚动添加行为.这是为RollingFileAppender滚动文件的现有方法,您可以覆盖该文件以添加存档.

You can create a custom appender inheriting from the RollingFileAppender, and override the AdjustFileBeforeAppend method in order to add behavior to the file rolling. Here is the existing method that rolls the file for the RollingFileAppender that you can override to add archiving.

使用File属性找到要处理的文件名

Use the File property to find the file name process it

// log4net.Appender.RollingFileAppender
protected virtual void AdjustFileBeforeAppend()
{
    var fileToZip = File; // save the current file
    if (this.m_rollDate)
    {
        DateTime now = this.m_dateTime.Now;
        if (now >= this.m_nextCheck)
        {
            this.m_now = now;
            this.m_nextCheck = this.NextCheckDate(this.m_now, this.m_rollPoint);
            this.RollOverTime(true);
            // zip the file if roll occurs here
        }
    }
    if (this.m_rollSize)
    {
        if (this.File != null && ((CountingQuietTextWriter)base.QuietWriter).Count >= this.m_maxFileSize)
        {
            this.RollOverSize();
            // zip the file if roll occurs here
        }
    }
}

或者,您可以找到一个可以满足您需要的附加程序,但我不知道.

Alternatively you could find an existing appender that does what you want, but I don't know of any.

由于他提出的建议相当优雅,因此我可以自由地将@stuartd的评论添加到答案中.您可以通过以下方式简单地覆盖AdjustFileBeforeAppend:

I'm taking the liberty of lifting @stuartd's comment into the answer since what he proposes is quite elegant. You can simply override the AdjustFileBeforeAppend in this way:

protected override void AdjustFileBeforeAppend() {
    var previousFile = File;
    base.AdjustFileBeforeAppend();
    if (File != previousFile) { // zip the file }
}

这是一种很整齐的方法,但是您可能希望能够区分两种类型的卷(即日期和大小).例如,仅将日期范围压缩才能将日期范围内的文件保持在一起.

It is quite a neat way of doing it, but you may want to be able to differentiate between both kind of rolls (ie date and size). For example only zipping on date roll in order to keep the files for a date range together.

这篇关于如何使用Log4Net对日志文件实施自动归档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆