| @@ -1,11 +1,13 @@ | |||
| <?xml version="1.0" encoding="utf-8" ?> | |||
| <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" | |||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
| autoReload="true"> | |||
| <!-- Warning: Configuration may reset after shadowsocks upgrade. --> | |||
| <!-- If you messed it up, delete this file and Shadowsocks will create a new one. --> | |||
| <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |||
| <targets> | |||
| <target name="file" xsi:type="File" fileName="shadowsocks.log"/> | |||
| <!-- This line is managed by Shadowsocks. Do not modify it unless you know what you are doing.--> | |||
| <target name="file" xsi:type="File" fileName="ss_win_temp\shadowsocks.log" writeBom="false" encoding="utf-8"/> | |||
| </targets> | |||
| <rules> | |||
| <!-- This line is managed by Shadowsocks. Do not modify it unless you know what you are doing. --> | |||
| <logger name="*" minlevel="Info" writeTo="file"/> | |||
| </rules> | |||
| </nlog> | |||
| @@ -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) | |||
| @@ -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; | |||
| /// <summary> | |||
| /// 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 | |||
| /// <returns></returns> | |||
| public string GetLogFileName() | |||
| { | |||
| return logFileNameElement.GetAttribute(FILE_NAME_ATTRIBUTE); | |||
| return logFileNameElement.GetAttribute(LOGGER_FILE_NAME_ATTRIBUTE); | |||
| } | |||
| /// <summary> | |||
| @@ -77,7 +77,7 @@ namespace Shadowsocks.Model | |||
| /// <param name="logLevel"></param> | |||
| public void SetLogLevel(LogLevel logLevel) | |||
| { | |||
| logLevelElement.SetAttribute(MIN_LEVEL_ATTRIBUTE, logLevel.ToString("G")); | |||
| logLevelElement.SetAttribute(TARGET_MIN_LEVEL_ATTRIBUTE, logLevel.ToString("G")); | |||
| } | |||
| /// <summary> | |||
| @@ -86,9 +86,15 @@ namespace Shadowsocks.Model | |||
| /// <param name="fileName"></param> | |||
| public void SetLogFileName(string fileName) | |||
| { | |||
| logFileNameElement.SetAttribute(FILE_NAME_ATTRIBUTE, fileName); | |||
| logFileNameElement.SetAttribute(LOGGER_FILE_NAME_ATTRIBUTE, fileName); | |||
| } | |||
| /// <summary> | |||
| /// Select a single XML node/elemant | |||
| /// </summary> | |||
| /// <param name="doc"></param> | |||
| /// <param name="xpath"></param> | |||
| /// <returns></returns> | |||
| private static XmlNode SelectSingleNode(XmlDocument doc, string xpath) | |||
| { | |||
| XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable); | |||
| @@ -109,6 +115,9 @@ namespace Shadowsocks.Model | |||
| } | |||
| } | |||
| /// <summary> | |||
| /// NLog reload the config file and apply to current LogManager | |||
| /// </summary> | |||
| public static void LoadConfiguration() | |||
| { | |||
| LogManager.LoadConfiguration(NLOG_CONFIG_FILE_NAME); | |||
| @@ -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)."); | |||
| } | |||
| @@ -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) | |||