|
| 1 | +using NLog; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using System.Xml; |
| 9 | + |
| 10 | +namespace Shadowsocks.Model |
| 11 | +{ |
| 12 | + public class NLogConfig |
| 13 | + { |
| 14 | + public enum LogLevel |
| 15 | + { |
| 16 | + Fatal, |
| 17 | + Error, |
| 18 | + Warn, |
| 19 | + Info, |
| 20 | + Debug, |
| 21 | + Trace, |
| 22 | + } |
| 23 | + |
| 24 | + const string NLOG_CONFIG_FILE_NAME = "NLog.config"; |
| 25 | + const string MIN_LEVEL_ATTRIBUTE = "minlevel"; |
| 26 | + const string FILE_NAME_ATTRIBUTE = "fileName"; |
| 27 | + |
| 28 | + XmlDocument doc = new XmlDocument(); |
| 29 | + XmlElement logLevelElement; |
| 30 | + XmlElement logFileNameElement; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Load the NLog config xml file content |
| 34 | + /// </summary> |
| 35 | + public static NLogConfig LoadXML() |
| 36 | + { |
| 37 | + NLogConfig config = new NLogConfig(); |
| 38 | + config.doc.Load(NLOG_CONFIG_FILE_NAME); |
| 39 | + config.logLevelElement = (XmlElement)SelectSingleNode(config.doc, "//nlog:logger[@name='*']"); |
| 40 | + config.logFileNameElement = (XmlElement)SelectSingleNode(config.doc, "//nlog:target[@name='file']"); |
| 41 | + return config; |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Save the content to NLog config xml file |
| 46 | + /// </summary> |
| 47 | + public static void SaveXML(NLogConfig nLogConfig) |
| 48 | + { |
| 49 | + nLogConfig.doc.Save(NLOG_CONFIG_FILE_NAME); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Get the current minLogLevel from xml file |
| 55 | + /// </summary> |
| 56 | + /// <returns></returns> |
| 57 | + public LogLevel GetLogLevel() |
| 58 | + { |
| 59 | + LogLevel level = LogLevel.Warn; |
| 60 | + string levelStr = logLevelElement.GetAttribute(MIN_LEVEL_ATTRIBUTE); |
| 61 | + Enum.TryParse(levelStr, out level); |
| 62 | + return level; |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Get the target fileName from xml file |
| 67 | + /// </summary> |
| 68 | + /// <returns></returns> |
| 69 | + public string GetLogFileName() |
| 70 | + { |
| 71 | + return logFileNameElement.GetAttribute(FILE_NAME_ATTRIBUTE); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Set the minLogLevel to xml file |
| 76 | + /// </summary> |
| 77 | + /// <param name="logLevel"></param> |
| 78 | + public void SetLogLevel(LogLevel logLevel) |
| 79 | + { |
| 80 | + logLevelElement.SetAttribute(MIN_LEVEL_ATTRIBUTE, logLevel.ToString("G")); |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Set the target fileName to xml file |
| 85 | + /// </summary> |
| 86 | + /// <param name="fileName"></param> |
| 87 | + public void SetLogFileName(string fileName) |
| 88 | + { |
| 89 | + logFileNameElement.SetAttribute(FILE_NAME_ATTRIBUTE, fileName); |
| 90 | + } |
| 91 | + |
| 92 | + private static XmlNode SelectSingleNode(XmlDocument doc, string xpath) |
| 93 | + { |
| 94 | + XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable); |
| 95 | + manager.AddNamespace("nlog", "http://www.nlog-project.org/schemas/NLog.xsd"); |
| 96 | + //return doc.SelectSingleNode("//nlog:logger[(@shadowsocks='managed') and (@name='*')]", manager); |
| 97 | + return doc.SelectSingleNode(xpath, manager); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Extract the pre-defined NLog configuration file is does not exist. Then reload the Nlog configuration. |
| 102 | + /// </summary> |
| 103 | + public static void TouchAndApplyNLogConfig() |
| 104 | + { |
| 105 | + if (!File.Exists(NLOG_CONFIG_FILE_NAME)) |
| 106 | + { |
| 107 | + File.WriteAllText(NLOG_CONFIG_FILE_NAME, Properties.Resources.NLog_config); |
| 108 | + LogManager.LoadConfiguration(NLOG_CONFIG_FILE_NAME); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public static void LoadConfiguration() |
| 113 | + { |
| 114 | + LogManager.LoadConfiguration(NLOG_CONFIG_FILE_NAME); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments