Browse Source

🧻 Remove `NLog.config` in favor of JSON

pull/3073/head
database64128 3 years ago
parent
commit
d126875399
No known key found for this signature in database GPG Key ID: 1CA27546BEDB8B01
5 changed files with 3 additions and 173 deletions
  1. +0
    -137
      Shadowsocks.WPF/Models/NlogConfig.cs
  2. +0
    -15
      Shadowsocks.WPF/Properties/Resources.Designer.cs
  3. +3
    -6
      Shadowsocks.WPF/Properties/Resources.resx
  4. +0
    -13
      Shadowsocks.WPF/Resources/NLog.config
  5. +0
    -2
      Shadowsocks.WPF/Shadowsocks.WPF.csproj

+ 0
- 137
Shadowsocks.WPF/Models/NlogConfig.cs View File

@@ -1,137 +0,0 @@
using NLog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Shadowsocks.WPF.Models
{
public class NLogConfig
{
public enum LogLevel
{
Fatal,
Error,
Warn,
Info,
Debug,
Trace,
}
private static string _NLOG_CONFIG_FILE_NAME=string.Empty;
public static string NLOG_CONFIG_FILE_NAME
{
get
{
if (string.IsNullOrEmpty(_NLOG_CONFIG_FILE_NAME))
{
_NLOG_CONFIG_FILE_NAME = Path.Combine(Environment.CurrentDirectory, "NLog.config");
}
return _NLOG_CONFIG_FILE_NAME;
}
}
const string TARGET_MIN_LEVEL_ATTRIBUTE = "minlevel";
const string LOGGER_FILE_NAME_ATTRIBUTE = "fileName";

XmlDocument doc = new XmlDocument();
XmlElement? logFileNameElement;
XmlElement? logLevelElement;

/// <summary>
/// Load the NLog config xml file content
/// </summary>
public static NLogConfig LoadXML()
{
NLogConfig config = new NLogConfig();
config.doc.Load(NLOG_CONFIG_FILE_NAME);
config.logLevelElement = (XmlElement?)SelectSingleNode(config.doc, "//nlog:logger[@name='*']");
config.logFileNameElement = (XmlElement?)SelectSingleNode(config.doc, "//nlog:target[@name='file']");
return config;
}

/// <summary>
/// Save the content to NLog config xml file
/// </summary>
public static void SaveXML(NLogConfig nLogConfig)
{
nLogConfig.doc.Save(NLOG_CONFIG_FILE_NAME);
}


/// <summary>
/// Get the current minLogLevel from xml file
/// </summary>
/// <returns></returns>
public LogLevel GetLogLevel()
{
LogLevel level = LogLevel.Warn;
string levelStr = logLevelElement.GetAttribute(TARGET_MIN_LEVEL_ATTRIBUTE);
Enum.TryParse(levelStr, out level);
return level;
}

/// <summary>
/// Get the target fileName from xml file
/// </summary>
/// <returns></returns>
public string GetLogFileName()
{
return logFileNameElement.GetAttribute(LOGGER_FILE_NAME_ATTRIBUTE);
}

/// <summary>
/// Set the minLogLevel to xml file
/// </summary>
/// <param name="logLevel"></param>
public void SetLogLevel(LogLevel logLevel)
{
logLevelElement.SetAttribute(TARGET_MIN_LEVEL_ATTRIBUTE, logLevel.ToString("G"));
}

/// <summary>
/// Set the target fileName to xml file
/// </summary>
/// <param name="fileName"></param>
public void SetLogFileName(string 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);
manager.AddNamespace("nlog", "http://www.nlog-project.org/schemas/NLog.xsd");
//return doc.SelectSingleNode("//nlog:logger[(@shadowsocks='managed') and (@name='*')]", manager);
return doc.SelectSingleNode(xpath, manager);
}

/// <summary>
/// Extract the pre-defined NLog configuration file is does not exist. Then reload the Nlog configuration.
/// </summary>
public static void TouchAndApplyNLogConfig()
{
if (!File.Exists(NLOG_CONFIG_FILE_NAME))
{
File.WriteAllText(NLOG_CONFIG_FILE_NAME, Properties.Resources.NLog);
LogManager.LoadConfiguration(NLOG_CONFIG_FILE_NAME);
}
}

/// <summary>
/// NLog reload the config file and apply to current LogManager
/// </summary>
public static void LoadConfiguration()
{
LogManager.LoadConfiguration(NLOG_CONFIG_FILE_NAME);
}
}
}

+ 0
- 15
Shadowsocks.WPF/Properties/Resources.Designer.cs View File

@@ -60,21 +60,6 @@ namespace Shadowsocks.WPF.Properties {
} }
} }
/// <summary>
/// Looks up a localized string similar to &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
///&lt;!-- Warning: Configuration may reset after shadowsocks upgrade. --&gt;
///&lt;!-- If you messed it up, delete this file and Shadowsocks will create a new one. --&gt;
///&lt;nlog xmlns=&quot;http://www.nlog-project.org/schemas/NLog.xsd&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;
/// &lt;targets&gt;
/// &lt;!-- This line is managed by Shadowsocks. Do not modify it unless you know what you are doing.--&gt;
/// &lt;target name=&quot;file&quot; xsi:type=&quot;File&quot; fileName=&quot;ss_win_temp\shadowsocks [rest of string was truncated]&quot;;.
/// </summary>
internal static string NLog {
get {
return ResourceManager.GetString("NLog", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to listen-address __PRIVOXY_BIND_IP__:__PRIVOXY_BIND_PORT__ /// Looks up a localized string similar to listen-address __PRIVOXY_BIND_IP__:__PRIVOXY_BIND_PORT__
///toggle 0 ///toggle 0


+ 3
- 6
Shadowsocks.WPF/Properties/Resources.resx View File

@@ -112,15 +112,12 @@
<value>2.0</value> <value>2.0</value>
</resheader> </resheader>
<resheader name="reader"> <resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="NLog" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\NLog.config;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="privoxy_conf" type="System.Resources.ResXFileRef, System.Windows.Forms"> <data name="privoxy_conf" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\privoxy_conf.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value> <value>..\Resources\privoxy_conf.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data> </data>


+ 0
- 13
Shadowsocks.WPF/Resources/NLog.config View File

@@ -1,13 +0,0 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- 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>
<!-- 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>

+ 0
- 2
Shadowsocks.WPF/Shadowsocks.WPF.csproj View File

@@ -19,7 +19,6 @@
</PropertyGroup> </PropertyGroup>


<ItemGroup> <ItemGroup>
<None Remove="Resources\NLog.config" />
<None Remove="Resources\privoxy.exe.gz" /> <None Remove="Resources\privoxy.exe.gz" />
<None Remove="Resources\privoxy_conf.txt" /> <None Remove="Resources\privoxy_conf.txt" />
<None Remove="Resources\RobotoMono\RobotoMono-Bold.ttf" /> <None Remove="Resources\RobotoMono\RobotoMono-Bold.ttf" />
@@ -96,7 +95,6 @@
</ItemGroup> </ItemGroup>


<ItemGroup> <ItemGroup>
<Resource Include="Resources\NLog.config" />
<Resource Include="Resources\privoxy.exe.gz" /> <Resource Include="Resources\privoxy.exe.gz" />
<Resource Include="Resources\privoxy_conf.txt" /> <Resource Include="Resources\privoxy_conf.txt" />
<Resource Include="Resources\RobotoMono\RobotoMono-Bold.ttf" /> <Resource Include="Resources\RobotoMono\RobotoMono-Bold.ttf" />


Loading…
Cancel
Save