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.Model
{
public class NLogConfig
{
public enum LogLevel
{
Fatal,
Error,
Warn,
Info,
Debug,
Trace,
}
const string NLOG_CONFIG_FILE_NAME = "NLog.config";
const string MIN_LEVEL_ATTRIBUTE = "minlevel";
const string FILE_NAME_ATTRIBUTE = "fileName";
XmlDocument doc = new XmlDocument();
XmlElement logLevelElement;
XmlElement logFileNameElement;
///
/// Load the NLog config xml file content
///
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;
}
///
/// Save the content to NLog config xml file
///
public static void SaveXML(NLogConfig nLogConfig)
{
nLogConfig.doc.Save(NLOG_CONFIG_FILE_NAME);
}
///
/// Get the current minLogLevel from xml file
///
///
public LogLevel GetLogLevel()
{
LogLevel level = LogLevel.Warn;
string levelStr = logLevelElement.GetAttribute(MIN_LEVEL_ATTRIBUTE);
Enum.TryParse(levelStr, out level);
return level;
}
///
/// Get the target fileName from xml file
///
///
public string GetLogFileName()
{
return logFileNameElement.GetAttribute(FILE_NAME_ATTRIBUTE);
}
///
/// Set the minLogLevel to xml file
///
///
public void SetLogLevel(LogLevel logLevel)
{
logLevelElement.SetAttribute(MIN_LEVEL_ATTRIBUTE, logLevel.ToString("G"));
}
///
/// Set the target fileName to xml file
///
///
public void SetLogFileName(string fileName)
{
logFileNameElement.SetAttribute(FILE_NAME_ATTRIBUTE, fileName);
}
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);
}
///
/// Extract the pre-defined NLog configuration file is does not exist. Then reload the Nlog configuration.
///
public static void TouchAndApplyNLogConfig()
{
if (!File.Exists(NLOG_CONFIG_FILE_NAME))
{
File.WriteAllText(NLOG_CONFIG_FILE_NAME, Properties.Resources.NLog_config);
LogManager.LoadConfiguration(NLOG_CONFIG_FILE_NAME);
}
}
public static void LoadConfiguration()
{
LogManager.LoadConfiguration(NLOG_CONFIG_FILE_NAME);
}
}
}