【语言-c#】log4net自定义appender定时删除n天之前的日志文件

问题说明

log4net 2.0.8.0

Delete log4net files for 10 days
log4net自动删除日志文件

按照我的搜索习惯,先去log4net 官网查找相关资料,最终结果是log4net 官网所有章节并未提及到这个功能,于是我就 百度/必应 了log4net 自动删除指定天数之前日志的解决方案,于是在stackoverflow的网站上找到了一篇这样的提问文章 Can Log4Net Delete Log Files Automatically? ,此文章的疑问刚好与我的疑问想契合,在文章的评论区有大神给出了两个其他的解决方案,大概意思如下:

译文如下

1.创建自定义的 appender,继承于 RollingFileAppender类,如果你想拥有更多的控制,可以继承于 FileAppender类,然后使用自定义的appender 修改你的配置文件(修改appender元素下的内容)。

2.创建一个.bat的批处理文件,用来删除早于n天前的文件。然后在Windows下创建一个任务用来每天运行这个批处理文件。

原评论如下

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

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. 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.

 

扩展 RollingFileAppender

实现加载时删除指定时间之前的日志

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using log4net.Appender;

namespace ISail
{
    public class IRollingFileAppender : RollingFileAppender
    {

        /// 
        /// 删除模式
        /// 
        public enum DeleteMode
        {
            LastWriteTime = 0,
            CreationTime = 1,
            LastAccessTime = 2,
        }
        public int MaximumFileCount { get; set; }
        /// 
        /// 自动删除N天之前的日志
        /// 
        public int OutDateDays { get; set; }
        /// 
        /// 选择删除时的匹配条件
        /// 
        public DeleteMode DeleteStyle { get; set; }
        public override void ActivateOptions()
        {
            base.ActivateOptions();
            DeleteLogFilesForOutDate();
            DeleteLogFilesForOverCount();

        }
        /// 
        /// 删除当前日志所在目录下的文件
        /// 
        public void DeleteLogFilesForOutDate()
        {
            try
            {
                string strBasepath = System.IO.Path.GetDirectoryName(File);
                foreach (string file in Directory.GetFiles(strBasepath))
                {
                    DeleteLogFiles(file, DeleteStyle);
                }
            }
            catch { }
        }
        /// 
        /// 删除超过指定数量的日志
        /// 
        public void DeleteLogFilesForOverCount()
        {
            try
            {
                string strBasepath = System.IO.Path.GetDirectoryName(File);
                int a = MaxSizeRollBackups;
                int b = MaximumFileCount;
                int n = a > b ? a : b;

                DirectoryInfo di = new DirectoryInfo(strBasepath);
                FileInfo[] query = di.GetFiles();
                if (n >= query.Length) return;

                if (DeleteStyle == DeleteMode.CreationTime)
                {
                    var querySort = query.OrderByDescending(o => o.CreationTime).ToArray();
                    for (int i = n; i < querySort.Length; i++)
                    {
                        try
                        {
                            System.IO.File.Delete(querySort[i].FullName);
                        }
                        catch
                        {
                        }
                    }
                }
                else if (DeleteStyle == DeleteMode.LastAccessTime)
                {
                    var querySort = query.OrderByDescending(o => o.LastAccessTime).ToArray();
                    for (int i = n; i < querySort.Length; i++)
                    {
                        try
                        {
                            System.IO.File.Delete(query[i].FullName);
                        }
                        catch
                        {
                        }
                    }
                }
                else if (DeleteStyle == DeleteMode.LastWriteTime)
                {
                    var querySort = query.OrderByDescending(o => o.LastWriteTime).ToArray();
                    for (int i = n; i < querySort.Length; i++)
                    {
                        try
                        {
                            System.IO.File.Delete(querySort[i].FullName);
                        }
                        catch
                        {
                        }
                    }
                }

            }
            catch { }
        }
        /// 
        /// 根据时间条件删除文件
        /// 
        /// 
        /// 
        private void DeleteLogFiles(string file, DeleteMode mode = DeleteMode.LastWriteTime)
        {
            try
            {
                int odd = OutDateDays;
                if (string.IsNullOrWhiteSpace(file)) return;
                if (System.IO.File.Exists(file) == false) return;

                System.IO.FileInfo fi = new FileInfo(file);
                if (mode == DeleteMode.CreationTime && fi.CreationTime < DateTime.Now.AddDays(-odd))
                {
                    System.IO.File.Delete(file);
                }
                else if (mode == DeleteMode.LastWriteTime && fi.LastWriteTime < DateTime.Now.AddDays(-odd))
                {
                    System.IO.File.Delete(file);
                }
                else if (mode == DeleteMode.LastAccessTime && fi.LastAccessTime < DateTime.Now.AddDays(-odd))
                {
                    System.IO.File.Delete(file);
                }
            }
            catch { }
        }
    }
}

app.config 或 web.config

  
    
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
        
      
    
    
      
      
    
  

 

 

你可能感兴趣的:(语言-c#)