Browse Source

Refind code and comments

tags/4.1.9.3
celeron533 4 years ago
parent
commit
e15cfacacd
5 changed files with 45 additions and 18 deletions
  1. +6
    -4
      shadowsocks-csharp/Data/NLog.config
  2. +3
    -2
      shadowsocks-csharp/Model/Configuration.cs
  3. +16
    -7
      shadowsocks-csharp/Model/NlogConfig.cs
  4. +1
    -1
      shadowsocks-csharp/Util/Util.cs
  5. +19
    -4
      shadowsocks-csharp/View/LogForm.cs

+ 6
- 4
shadowsocks-csharp/Data/NLog.config View File

@@ -1,11 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?> <?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> <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> </targets>
<rules> <rules>
<!-- This line is managed by Shadowsocks. Do not modify it unless you know what you are doing. -->
<logger name="*" minlevel="Info" writeTo="file"/> <logger name="*" minlevel="Info" writeTo="file"/>
</rules> </rules>
</nlog> </nlog>

+ 3
- 2
shadowsocks-csharp/Model/Configuration.cs View File

@@ -127,7 +127,8 @@ namespace Shadowsocks.Model
} }
catch (Exception e) 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; return config;
@@ -179,7 +180,7 @@ namespace Shadowsocks.Model
} }
catch(Exception e) 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) catch (IOException e)


+ 16
- 7
shadowsocks-csharp/Model/NlogConfig.cs View File

@@ -22,12 +22,12 @@ namespace Shadowsocks.Model
} }
const string NLOG_CONFIG_FILE_NAME = "NLog.config"; 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(); XmlDocument doc = new XmlDocument();
XmlElement logLevelElement;
XmlElement logFileNameElement; XmlElement logFileNameElement;
XmlElement logLevelElement;
/// <summary> /// <summary>
/// Load the NLog config xml file content /// Load the NLog config xml file content
@@ -57,7 +57,7 @@ namespace Shadowsocks.Model
public LogLevel GetLogLevel() public LogLevel GetLogLevel()
{ {
LogLevel level = LogLevel.Warn; LogLevel level = LogLevel.Warn;
string levelStr = logLevelElement.GetAttribute(MIN_LEVEL_ATTRIBUTE);
string levelStr = logLevelElement.GetAttribute(TARGET_MIN_LEVEL_ATTRIBUTE);
Enum.TryParse(levelStr, out level); Enum.TryParse(levelStr, out level);
return level; return level;
} }
@@ -68,7 +68,7 @@ namespace Shadowsocks.Model
/// <returns></returns> /// <returns></returns>
public string GetLogFileName() public string GetLogFileName()
{ {
return logFileNameElement.GetAttribute(FILE_NAME_ATTRIBUTE);
return logFileNameElement.GetAttribute(LOGGER_FILE_NAME_ATTRIBUTE);
} }
/// <summary> /// <summary>
@@ -77,7 +77,7 @@ namespace Shadowsocks.Model
/// <param name="logLevel"></param> /// <param name="logLevel"></param>
public void SetLogLevel(LogLevel logLevel) public void SetLogLevel(LogLevel logLevel)
{ {
logLevelElement.SetAttribute(MIN_LEVEL_ATTRIBUTE, logLevel.ToString("G"));
logLevelElement.SetAttribute(TARGET_MIN_LEVEL_ATTRIBUTE, logLevel.ToString("G"));
} }
/// <summary> /// <summary>
@@ -86,9 +86,15 @@ namespace Shadowsocks.Model
/// <param name="fileName"></param> /// <param name="fileName"></param>
public void SetLogFileName(string fileName) 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) private static XmlNode SelectSingleNode(XmlDocument doc, string xpath)
{ {
XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable); 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() public static void LoadConfiguration()
{ {
LogManager.LoadConfiguration(NLOG_CONFIG_FILE_NAME); LogManager.LoadConfiguration(NLOG_CONFIG_FILE_NAME);


+ 1
- 1
shadowsocks-csharp/Util/Util.cs View File

@@ -82,7 +82,7 @@ namespace Shadowsocks.Util
catch catch
{ {
logger.Info(
logger.Debug(
$"Cannot get Windows 10 system theme mode, return default value 0 (dark mode)."); $"Cannot get Windows 10 system theme mode, return default value 0 (dark mode).");
} }


+ 19
- 4
shadowsocks-csharp/View/LogForm.cs View File

@@ -49,7 +49,18 @@ namespace Shadowsocks.View
Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); Icon = Icon.FromHandle(Resources.ssw128.GetHicon());
var nLogConfig = NLogConfig.LoadXML(); 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; LogViewerConfig config = controller.GetConfigurationCopy().logViewer;
@@ -164,6 +175,8 @@ namespace Shadowsocks.View
private void InitContent() private void InitContent()
{ {
if (string.IsNullOrEmpty(filename))
return;
using (StreamReader reader = new StreamReader(new FileStream(filename, using (StreamReader reader = new StreamReader(new FileStream(filename,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{ {
@@ -187,6 +200,11 @@ namespace Shadowsocks.View
private void UpdateContent() 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 try
{ {
using (StreamReader reader = new StreamReader(new FileStream(filename, using (StreamReader reader = new StreamReader(new FileStream(filename,
@@ -215,9 +233,6 @@ namespace Shadowsocks.View
catch (FileNotFoundException) 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) private void LogForm_Load(object sender, EventArgs e)


Loading…
Cancel
Save