From 6a427a2158d4ce90542b62ff606a1bf45e17b8cc Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sun, 19 Dec 2021 20:47:58 +0800 Subject: [PATCH] Join log4net --- v2rayN/v2rayN/Program.cs | 2 +- v2rayN/v2rayN/Tool/Logging.cs | 37 +++++++++++++++++++++++++++++++++++ v2rayN/v2rayN/Tool/Utils.cs | 34 ++++++-------------------------- 3 files changed, 44 insertions(+), 29 deletions(-) create mode 100644 v2rayN/v2rayN/Tool/Logging.cs diff --git a/v2rayN/v2rayN/Program.cs b/v2rayN/v2rayN/Program.cs index 4d37d44f..a6dc5246 100644 --- a/v2rayN/v2rayN/Program.cs +++ b/v2rayN/v2rayN/Program.cs @@ -33,7 +33,7 @@ namespace v2rayN if (!IsDuplicateInstance()) { - + Logging.Setup(); Utils.SaveLog($"v2rayN start up | {Utils.GetVersion()} | {Utils.GetExePath()}"); //设置语言环境 diff --git a/v2rayN/v2rayN/Tool/Logging.cs b/v2rayN/v2rayN/Tool/Logging.cs new file mode 100644 index 00000000..39b6dd02 --- /dev/null +++ b/v2rayN/v2rayN/Tool/Logging.cs @@ -0,0 +1,37 @@ +using log4net; +using log4net.Appender; +using log4net.Core; +using log4net.Layout; +using log4net.Repository.Hierarchy; + +namespace v2rayN.Tool +{ + public class Logging + { + public static void Setup() + { + Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository(); + + 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; + roller.DatePattern = "yyyy-MM-dd'.txt'"; + roller.File = Utils.GetPath(@"guiLogs\"); + roller.Layout = patternLayout; + roller.StaticLogFileName = false; + roller.ActivateOptions(); + hierarchy.Root.AddAppender(roller); + + MemoryAppender memory = new MemoryAppender(); + memory.ActivateOptions(); + hierarchy.Root.AddAppender(memory); + + hierarchy.Root.Level = Level.Info; + hierarchy.Configured = true; + } + } +} diff --git a/v2rayN/v2rayN/Tool/Utils.cs b/v2rayN/v2rayN/Tool/Utils.cs index b6aed414..6196ce69 100644 --- a/v2rayN/v2rayN/Tool/Utils.cs +++ b/v2rayN/v2rayN/Tool/Utils.cs @@ -21,6 +21,7 @@ using System.Security.Principal; using v2rayN.Base; using Newtonsoft.Json.Linq; using System.Web; +using log4net; namespace v2rayN { @@ -870,37 +871,14 @@ namespace v2rayN public static void SaveLog(string strContent) { - SaveLog("info", new Exception(strContent)); + var logger = LogManager.GetLogger("Log1"); + logger.Info(strContent); } public static void SaveLog(string strTitle, Exception ex) { - try - { - string path = Path.Combine(StartupPath(), "guiLogs"); - string FilePath = Path.Combine(path, DateTime.Now.ToString("yyyyMMdd") + ".txt"); - if (!Directory.Exists(path)) - { - Directory.CreateDirectory(path); - } - if (!File.Exists(FilePath)) - { - FileStream FsCreate = new FileStream(FilePath, FileMode.Create); - FsCreate.Close(); - FsCreate.Dispose(); - } - FileStream FsWrite = new FileStream(FilePath, FileMode.Append, FileAccess.Write); - StreamWriter SwWrite = new StreamWriter(FsWrite); - - string strContent = ex.ToString(); - - SwWrite.WriteLine(string.Format("{0}{1}[{2}]{3}", "--------------------------------", strTitle, DateTime.Now.ToString("HH:mm:ss"), "--------------------------------")); - SwWrite.Write(strContent); - SwWrite.WriteLine(Environment.NewLine); - SwWrite.WriteLine(" "); - SwWrite.Flush(); - SwWrite.Close(); - } - catch { } + var logger = LogManager.GetLogger("Log2"); + logger.Debug(strTitle); + logger.Debug(ex); } #endregion