You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

NlogConfig.cs 4.5 kB

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