-1

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 ?

Nisha
  • 1,783
  • 4
  • 18
  • 34
  • Judith, you forgot to ask a question. What's going wrong here? What can't you fix? – spender Mar 05 '14 at 13:56
  • ok will update my question. – Nisha Mar 05 '14 at 13:57
  • If you intend your program to be redistributed and installed in "program files", you'll need to copy the config to a location where standard users have write access and get nlog to read it from there. If the config simply lives next to the .exe file in "program files" you should consider it to be readonly. – spender Mar 05 '14 at 13:59
  • well , the config file exists in a folder where the users have the permissions to edit the files. – Nisha Mar 05 '14 at 14:01

1 Answers1

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;
                        }
                    }
                }
            }
Nisha
  • 1,783
  • 4
  • 18
  • 34