资源简介

log4net,可以根据配置的时间长度,实现了自动删除过期的日志文件,欢迎大家使用或者分享更科学的方法。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net.Config;
using log4net;
using log4net.Appender;
using System.IO;
using System.xml;
using System.Threading;

namespace CSCC.Net.Common
{
    public class Log
    {
        private static log4net.ILog _log = null;
        /// 
        /// 日志文件过期时间(单位:天),默认半年=180天
        /// 

        private static int outdate_days = 180;

        private static object lockHelper = new object();
        private static log4net.ILog InnerLog
        {
            get
            {
                if (null == _log)
                {
                    lock (lockHelper)
                    {
                        if (null == _log)
                        {
                            try
                            {
                                string cfgfile = “log4net.config“;//当前(exe文件所在)目录
                                xmlConfigurator.Configure(new System.IO.FileInfo(cfgfile));
                                _log = LogManager.GetLogger(“Logging“);

                                #region 删除过期的日期文件
                                Thread th = new Thread(() =>
                                {
                                    try
                                    {
                                        DeleteOutdatefiles(_log cfgfile);
                                    }
                                    catch (Exception ex)
                                    {
                                    }
                                });
                                th.IsBackground = true;
                                th.Start();
                                #endregion
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                }
                return _log;
            }
        }
        /// 
        /// 删除过期的日志文件
        /// 

        private static void DeleteOutdatefiles(ILog logstring cfgfile)
        {
            if (log == null ||
                !log.IsInfoEnabled || 
                !log.IsErrorEnabled)
            {
                return;
            }

            try
            {
                xmlDocument xdoc = new xmlDocument();
                xdoc.Load(cfgfile);
                var node = xdoc.SelectSingleNode(“log4net“);
                //附带属性到log4net.config,只能在根节点设置添加。
                //这个附带属性设置还不能放到其它节点,放到其它节点log4net解析会报错(虽未影响结果,但看着不舒服)
                if (null != node || node.Attributes != null || node.Attributes.Count > 0)
                {
                    outdate_days = int.Parse(node.Attributes[“outdate_days“].Value);
                }
            }
            catch (Exception exx)
            {
            }
            var apps = log.Logger.Re

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-10-23 09:09  log4net.test\
     目录           0  2017-10-29 10:43  log4net.test\bin\
     目录           0  2017-10-27 10:22  log4net.test\bin\Debug\
     目录           0  2017-10-29 10:24  log4net.test\bin\Debug\log\
     文件         647  2017-10-29 10:27  log4net.test\bin\Debug\log4net.config
     文件      266240  2017-10-23 09:07  log4net.test\bin\Debug\log4net.dll
     文件       10240  2017-10-29 10:43  log4net.test\bin\Debug\log4net.test.exe
     文件       30208  2017-10-29 10:43  log4net.test\bin\Debug\log4net.test.pdb
     文件       11600  2017-10-25 10:19  log4net.test\bin\Debug\log4net.test.vshost.exe
     文件          70  2017-10-29 10:24  log4net.test\bin\Debug\log\log.txt
     文件         140  2017-10-25 10:19  log4net.test\bin\Debug\log\log.txt2017-10-25.txt
     文件         210  2017-10-26 10:13  log4net.test\bin\Debug\log\log.txt2017-10-26.txt
     文件         353  2017-10-27 10:23  log4net.test\bin\Debug\log\log.txt2017-10-27.txt
     文件          71  2017-10-28 10:23  log4net.test\bin\Debug\log\log.txt2017-10-28.txt
     文件       10964  2017-10-29 10:40  log4net.test\CsccLog.cs
     文件         647  2017-10-29 10:27  log4net.test\log4net.config
     文件      266240  2017-10-23 09:07  log4net.test\log4net.dll
     文件        2906  2017-10-23 09:13  log4net.test\log4net.test.csproj
     文件         865  2017-10-23 09:06  log4net.test\log4net.test.sln
     文件       24064  2017-10-29 10:43  log4net.test\log4net.test.suo
     目录           0  2017-10-23 09:06  log4net.test\obj\
     目录           0  2017-10-23 09:06  log4net.test\obj\x86\
     目录           0  2017-10-29 10:43  log4net.test\obj\x86\Debug\
     文件        5891  2017-10-29 10:43  log4net.test\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         445  2017-10-27 10:22  log4net.test\obj\x86\Debug\log4net.test.csproj.FileListAbsolute.txt
     文件       10240  2017-10-29 10:43  log4net.test\obj\x86\Debug\log4net.test.exe
     文件       30208  2017-10-29 10:43  log4net.test\obj\x86\Debug\log4net.test.pdb
     目录           0  2017-10-23 09:06  log4net.test\obj\x86\Debug\TempPE\
     文件         394  2017-10-29 10:43  log4net.test\Program.cs
     目录           0  2017-10-23 09:06  log4net.test\Properties\
     文件        1356  2017-10-23 09:06  log4net.test\Properties\AssemblyInfo.cs
............此处省略0个文件信息

评论

共有 条评论