From 82924278b5a0c9069f495a7184473db7673b9e42 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sun, 3 Apr 2022 17:01:45 +0800 Subject: [PATCH] Delete log files older than one month --- v2rayN/v2rayN/Program.cs | 3 ++- v2rayN/v2rayN/Tool/Logging.cs | 31 ++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/v2rayN/v2rayN/Program.cs b/v2rayN/v2rayN/Program.cs index a6dc5246..ace9a3c6 100644 --- a/v2rayN/v2rayN/Program.cs +++ b/v2rayN/v2rayN/Program.cs @@ -35,6 +35,7 @@ namespace v2rayN { Logging.Setup(); Utils.SaveLog($"v2rayN start up | {Utils.GetVersion()} | {Utils.GetExePath()}"); + Logging.ClearLogs(); //设置语言环境 string lang = Utils.RegReadValue(Global.MyRegPath, Global.MyRegKeyLanguage, "zh-Hans"); @@ -42,7 +43,7 @@ namespace v2rayN Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new MainForm()); + Application.Run(new MainForm()); } else { diff --git a/v2rayN/v2rayN/Tool/Logging.cs b/v2rayN/v2rayN/Tool/Logging.cs index 39b6dd02..ea931456 100644 --- a/v2rayN/v2rayN/Tool/Logging.cs +++ b/v2rayN/v2rayN/Tool/Logging.cs @@ -3,6 +3,9 @@ using log4net.Appender; using log4net.Core; using log4net.Layout; using log4net.Repository.Hierarchy; +using System; +using System.IO; +using System.Threading.Tasks; namespace v2rayN.Tool { @@ -15,7 +18,7 @@ namespace v2rayN.Tool PatternLayout patternLayout = new PatternLayout(); patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline"; patternLayout.ActivateOptions(); - + RollingFileAppender roller = new RollingFileAppender(); roller.AppendToFile = true; roller.RollingStyle = RollingFileAppender.RollingMode.Date; @@ -33,5 +36,31 @@ namespace v2rayN.Tool hierarchy.Root.Level = Level.Info; hierarchy.Configured = true; } + + public static void ClearLogs() + { + Task.Run(() => + { + try + { + var now = DateTime.Now.AddMonths(-1); + var dir = Utils.GetPath(@"guiLogs\"); + var files = Directory.GetFiles(dir, "*.txt"); + foreach (var filePath in files) + { + var file = new FileInfo(filePath); + if (file.CreationTime < now) + { + try + { + file.Delete(); + } + catch { } + } + } + } + catch { } + }); + } } }