From e15cfacacd4901ea22e83b39eaee2e93ac70f5d5 Mon Sep 17 00:00:00 2001 From: celeron533 Date: Mon, 27 Jan 2020 17:33:58 +0800 Subject: [PATCH] Refind code and comments --- shadowsocks-csharp/Data/NLog.config | 10 ++++++---- shadowsocks-csharp/Model/Configuration.cs | 5 +++-- shadowsocks-csharp/Model/NlogConfig.cs | 23 ++++++++++++++++------- shadowsocks-csharp/Util/Util.cs | 2 +- shadowsocks-csharp/View/LogForm.cs | 23 +++++++++++++++++++---- 5 files changed, 45 insertions(+), 18 deletions(-) diff --git a/shadowsocks-csharp/Data/NLog.config b/shadowsocks-csharp/Data/NLog.config index 4f9d27e3..05509cff 100644 --- a/shadowsocks-csharp/Data/NLog.config +++ b/shadowsocks-csharp/Data/NLog.config @@ -1,11 +1,13 @@  - + + + - + + + \ No newline at end of file diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index db686288..200cd20c 100644 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -127,7 +127,8 @@ namespace Shadowsocks.Model } catch (Exception e) { - logger.Error(e, "Cannot get the log level from NLog config file."); + // todo: route the error to UI since there is no log file in this scenario + logger.Error(e, "Cannot get the log level from NLog config file. Please check if the nlog config file exists with corresponding XML nodes."); } return config; @@ -179,7 +180,7 @@ namespace Shadowsocks.Model } catch(Exception e) { - logger.Error(e, "Cannot set the log level"); + logger.Error(e, "Cannot set the log level to NLog config file. Please check if the nlog config file exists with corresponding XML nodes."); } } catch (IOException e) diff --git a/shadowsocks-csharp/Model/NlogConfig.cs b/shadowsocks-csharp/Model/NlogConfig.cs index fe460a5f..11d6d858 100644 --- a/shadowsocks-csharp/Model/NlogConfig.cs +++ b/shadowsocks-csharp/Model/NlogConfig.cs @@ -22,12 +22,12 @@ namespace Shadowsocks.Model } const string NLOG_CONFIG_FILE_NAME = "NLog.config"; - const string MIN_LEVEL_ATTRIBUTE = "minlevel"; - const string FILE_NAME_ATTRIBUTE = "fileName"; + const string TARGET_MIN_LEVEL_ATTRIBUTE = "minlevel"; + const string LOGGER_FILE_NAME_ATTRIBUTE = "fileName"; XmlDocument doc = new XmlDocument(); - XmlElement logLevelElement; XmlElement logFileNameElement; + XmlElement logLevelElement; /// /// Load the NLog config xml file content @@ -57,7 +57,7 @@ namespace Shadowsocks.Model public LogLevel GetLogLevel() { LogLevel level = LogLevel.Warn; - string levelStr = logLevelElement.GetAttribute(MIN_LEVEL_ATTRIBUTE); + string levelStr = logLevelElement.GetAttribute(TARGET_MIN_LEVEL_ATTRIBUTE); Enum.TryParse(levelStr, out level); return level; } @@ -68,7 +68,7 @@ namespace Shadowsocks.Model /// public string GetLogFileName() { - return logFileNameElement.GetAttribute(FILE_NAME_ATTRIBUTE); + return logFileNameElement.GetAttribute(LOGGER_FILE_NAME_ATTRIBUTE); } /// @@ -77,7 +77,7 @@ namespace Shadowsocks.Model /// public void SetLogLevel(LogLevel logLevel) { - logLevelElement.SetAttribute(MIN_LEVEL_ATTRIBUTE, logLevel.ToString("G")); + logLevelElement.SetAttribute(TARGET_MIN_LEVEL_ATTRIBUTE, logLevel.ToString("G")); } /// @@ -86,9 +86,15 @@ namespace Shadowsocks.Model /// public void SetLogFileName(string fileName) { - logFileNameElement.SetAttribute(FILE_NAME_ATTRIBUTE, fileName); + logFileNameElement.SetAttribute(LOGGER_FILE_NAME_ATTRIBUTE, fileName); } + /// + /// Select a single XML node/elemant + /// + /// + /// + /// private static XmlNode SelectSingleNode(XmlDocument doc, string xpath) { XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable); @@ -109,6 +115,9 @@ namespace Shadowsocks.Model } } + /// + /// NLog reload the config file and apply to current LogManager + /// public static void LoadConfiguration() { LogManager.LoadConfiguration(NLOG_CONFIG_FILE_NAME); diff --git a/shadowsocks-csharp/Util/Util.cs b/shadowsocks-csharp/Util/Util.cs index dffd5271..8912eb68 100755 --- a/shadowsocks-csharp/Util/Util.cs +++ b/shadowsocks-csharp/Util/Util.cs @@ -82,7 +82,7 @@ namespace Shadowsocks.Util catch { - logger.Info( + logger.Debug( $"Cannot get Windows 10 system theme mode, return default value 0 (dark mode)."); } diff --git a/shadowsocks-csharp/View/LogForm.cs b/shadowsocks-csharp/View/LogForm.cs index 16f2ad6e..87dffa4b 100644 --- a/shadowsocks-csharp/View/LogForm.cs +++ b/shadowsocks-csharp/View/LogForm.cs @@ -49,7 +49,18 @@ namespace Shadowsocks.View Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); var nLogConfig = NLogConfig.LoadXML(); - this.filename = nLogConfig.GetLogFileName(); + try + { + this.filename = nLogConfig.GetLogFileName(); + } + catch(Exception) + { + // failed to get the file name + } + if (string.IsNullOrEmpty(this.filename)) + { + LogMessageTextBox.AppendText("Cannot get the log file name from NLog config file. Please check if the nlog config file exists with corresponding XML nodes."); + } LogViewerConfig config = controller.GetConfigurationCopy().logViewer; @@ -164,6 +175,8 @@ namespace Shadowsocks.View private void InitContent() { + if (string.IsNullOrEmpty(filename)) + return; using (StreamReader reader = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { @@ -187,6 +200,11 @@ namespace Shadowsocks.View private void UpdateContent() { + this.Text = I18N.GetString("Log Viewer") + + $" [in: {Utils.FormatBytes(controller.InboundCounter)}, out: {Utils.FormatBytes(controller.OutboundCounter)}]"; + + if (string.IsNullOrEmpty(filename)) + return; try { using (StreamReader reader = new StreamReader(new FileStream(filename, @@ -215,9 +233,6 @@ namespace Shadowsocks.View catch (FileNotFoundException) { } - - this.Text = I18N.GetString("Log Viewer") + - $" [in: {Utils.FormatBytes(controller.InboundCounter)}, out: {Utils.FormatBytes(controller.OutboundCounter)}]"; } private void LogForm_Load(object sender, EventArgs e)