如何以编程方式编辑nlog配置文件 [英] how to edit the nlog config file programmatically
问题描述
我正在尝试以编程方式在配置文件中编辑日志记录级别.
i am trying to edit the logging level in the config file programmaticly.
foreach (var rule in LogManager.Configuration.LoggingRules)
{
if (m_loginglevelcomboBox.SelectedItem.ToString() == "Debug")
{
rule.EnableLoggingForLevel(LogLevel.Debug);
}
else
{
rule.EnableLoggingForLevel(LogLevel.Info);
}
}
//LogManager.ReconfigExistingLoggers();
我对调用Reconfig并不感兴趣,因为更改会即时影响应用程序. 我希望在重新启动应用程序时进行更改.所以我需要它来编辑配置文件.
I am not interested in calling the Reconfig,as the changes will affect the application on the fly. I want the changes to be made when the application is restarted. so I need it to edit the config file.
我不能使用xDocument,因为linq与我的.net版本不兼容 所以我该如何编辑最小级别规则来调试/信息?
i cant use xDocument ,as linq is not compatible with my .net version so how can i edit the minlevel rule to debug/info ?
推荐答案
i用它来编辑日志记录级别.我希望如果有人偶然发现这对您有帮助.如果有人认为这是个坏主意,请告诉我.
i used this to edit the logging level. I hope if this would help if some one stumbles across. If some one thinks it to be a bad idea, please let me know .
string configFilename = GetConfigFilePath();
XmlDocument doc = new XmlDocument();
doc.Load(configFilename);
XmlNode documentElement = doc.DocumentElement;
foreach (XmlNode node in documentElement.ChildNodes)
{
if (ruleDocumentNodeName.Equals(node.Name))
{
foreach (XmlNode childNode in node.ChildNodes)
{
if (loggerDocumentNodeName.Equals(childNode.Name))
{
XmlAttribute idAttribute = childNode.Attributes[minLevelAttributeName];
string currentValue = minLogingLevelComboBox.SelectedItem.ToString();
idAttribute.Value = currentValue;
doc.Save(configFilename);
MinLoggingLevelChanged = true;
}
}
}
}
这篇关于如何以编程方式编辑nlog配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!