how to edit the nlog config file programmatically

戏子无情 提交于 2020-01-06 17:55:45

问题


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();

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.

i cant use xDocument ,as linq is not compatible with my .net version so how can i edit the minlevel rule to debug/info ?


回答1:


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


来源:https://stackoverflow.com/questions/22199507/how-to-edit-the-nlog-config-file-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!