zoukankan      html  css  js  c++  java
  • log4net自动删除日志文件

    晚上关于删除的日志文件主要有 https://blog.csdn.net/hellolib/article/details/78316253, 其实我这里的实现方式和他差不多,不过我这里不用配置时间, 直接写死 删除10天以前的日志数据,

      static ILog _log = null;
            static object lockHelper = new object();
            private ILog GetLog()
            {
     
                if (null == _log)
                {
                    lock (lockHelper)
                    {
                        if (null == _log)
                        {
                            StackTrace stackTrace = new StackTrace(this._skipFrames);
                            StackFrame stackFrame = stackTrace.GetFrame(0);
                            MethodBase methodBase = stackFrame.GetMethod();
     
                            // ApplicationRollingFile在.config文件中配置的名称
                            _log = LogManager.GetLogger("ApplicationRollingFile");
                            Task.Run(()=> {
                                var apps = _log.Logger.Repository.GetAppenders();
                                if (apps.Length <= 0)
                                {
                                    return;
                                }
                                var now = DateTime.UtcNow.AddDays(-10);
                                foreach (var item in apps)
                                {
                                    if (item is RollingFileAppender)
                                    {
                                        RollingFileAppender roll = item as RollingFileAppender;
                                        var dir = Path.GetDirectoryName(roll.File);
                                        var files = Directory.GetFiles(dir, "*.txt");
                                        //var sample = "log.txt2017-10-23.txt";
     
                                        foreach (var filePath in files)
                                        {
                                            var file = new FileInfo(filePath);
                                            if (file.CreationTimeUtc < now) {
                                                try
                                                {
                                                    file.Delete();
                                                }
                                                catch (Exception)
                                                {
     
                                                }
                                            }
                                        }
                                    }
                                }
                            });
                        }
                    }
                }
                            
                return _log;
            }

  • 相关阅读:
    简述密码
    w命令
    Python打包-Pyinstaller
    [转]油猴Tampermonkey-让百度云下载飞起来
    百度云高速下载Pandownload
    AdoConnect-获取连接字符串 (工具)
    Python爬虫-豆瓣电影 Top 250
    C++中使用ODBC API访问数据库例程
    Python爬虫-什么是爬虫?
    Python+Django(Python Web项目初体验)
  • 原文地址:https://www.cnblogs.com/majiang/p/11156883.html
Copyright © 2011-2022 走看看