【问题标题】:how to edit the nlog config file programmatically如何以编程方式编辑 nlog 配置文件
【发布时间】:2014-03-05 13:42:00
【问题描述】:

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

 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 不感兴趣,因为更改会即时影响应用程序。 我希望在重新启动应用程序时进行更改。所以我需要它来编辑配置文件。

我无法使用 xDocument,因为 linq 与我的 .net 版本不兼容 那么如何编辑 minlevel 规则来调试/信息?

【问题讨论】:

  • 朱迪思,你忘了问问题。这里出了什么问题?你不能解决什么问题?
  • ok 会更新我的问题。
  • 如果您打算将您的程序重新分发并安装在“程序文件”中,您需要将配置复制到标准用户具有写入权限的位置并让 nlog 从那里读取它。如果配置只是位于“程序文件”中的 .exe 文件旁边,您应该认为它是只读的。
  • 嗯,配置文件存在于用户有权编辑文件的文件夹中。

标签: c# nlog


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 2013-09-12
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多