From 7df90a6034c16db91ba24f5d05ed790faba236f6 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Mon, 28 Oct 2024 09:16:24 +0800 Subject: [PATCH] Bug fix https://github.com/2dust/v2rayN/issues/5928 --- v2rayN/ServiceLib/Common/Logging.cs | 41 +++++++++++++------------ v2rayN/ServiceLib/Handler/AppHandler.cs | 2 +- v2rayN/ServiceLib/Models/ConfigItems.cs | 2 ++ 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/v2rayN/ServiceLib/Common/Logging.cs b/v2rayN/ServiceLib/Common/Logging.cs index c212fd17..c8b07053 100644 --- a/v2rayN/ServiceLib/Common/Logging.cs +++ b/v2rayN/ServiceLib/Common/Logging.cs @@ -37,40 +37,41 @@ namespace ServiceLib.Common foreach (var filePath in files) { var file = new FileInfo(filePath); - if (file.CreationTime < now) + if (file.CreationTime >= now) continue; + try { - try - { - file.Delete(); - } - catch { } + file.Delete(); + } + catch + { + // ignored } } } - catch { } + catch + { + // ignored + } }); } public static void SaveLog(string strContent) { - if (LogManager.IsLoggingEnabled()) - { - var logger = LogManager.GetLogger("Log1"); - logger.Info(strContent); - } + if (!LogManager.IsLoggingEnabled()) return; + + LogManager.GetLogger("Log1").Info(strContent); } public static void SaveLog(string strTitle, Exception ex) { - if (LogManager.IsLoggingEnabled()) + if (!LogManager.IsLoggingEnabled()) return; + + var logger = LogManager.GetLogger("Log2"); + logger.Debug($"{strTitle},{ex.Message}"); + logger.Debug(ex.StackTrace); + if (ex?.InnerException != null) { - var logger = LogManager.GetLogger("Log2"); - logger.Debug($"{strTitle},{ex.Message}"); - logger.Debug(ex.StackTrace); - if (ex?.InnerException != null) - { - logger.Error(ex.InnerException); - } + logger.Error(ex.InnerException); } } } diff --git a/v2rayN/ServiceLib/Handler/AppHandler.cs b/v2rayN/ServiceLib/Handler/AppHandler.cs index e27634fa..1565a634 100644 --- a/v2rayN/ServiceLib/Handler/AppHandler.cs +++ b/v2rayN/ServiceLib/Handler/AppHandler.cs @@ -71,7 +71,7 @@ public bool InitComponents() { Logging.Setup(); - Logging.LoggingEnabled(true); + Logging.LoggingEnabled(_config.GuiItem.EnableLog); Logging.SaveLog($"v2rayN start up | {Utils.GetVersion()} | {Utils.GetExePath()}"); Logging.SaveLog($"{Environment.OSVersion} - {(Environment.Is64BitOperatingSystem ? 64 : 32)}"); Logging.ClearLogs(); diff --git a/v2rayN/ServiceLib/Models/ConfigItems.cs b/v2rayN/ServiceLib/Models/ConfigItems.cs index 42333bce..08414081 100644 --- a/v2rayN/ServiceLib/Models/ConfigItems.cs +++ b/v2rayN/ServiceLib/Models/ConfigItems.cs @@ -88,6 +88,8 @@ public int TrayMenuServersLimit { get; set; } = 20; public bool EnableHWA { get; set; } = false; + + public bool EnableLog { get; set; } = true; } [Serializable]