如何以编程方式编辑 NLOG 配置文件



>我正在尝试以编程方式编辑配置文件中的日志记录级别。

 foreach (var rule in LogManager.Configuration.LoggingRules)
    {

                if (m_loginglevelcomboBox.SelectedItem.ToString() == "Debug")
                {
                    rule.EnableLoggingForLevel(LogLevel.Debug);
                }
                else
                {
                    rule.EnableLoggingForLevel(LogLevel.Info);
                }
     }
            //LogManager.ReconfigExistingLoggers();

我对调用重新配置不感兴趣,因为更改将即时影响应用程序。我希望在重新启动应用程序时进行更改。所以我需要它来编辑配置文件。

我不能使用xDocument,因为linq与我的.net版本不兼容那么如何编辑最小级别规则以进行调试/信息?

我用它来编辑日志记录级别。我希望如果有人偶然发现,这是否会有所帮助。如果有人认为这是一个坏主意,请告诉我。

            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;
                        }
                    }
                }
            }

最新更新