You can refresh your own section the way you say:
ConfigurationManager.RefreshSection("yoursection/subsection");
Just move a logging true/false into a section and you'll be fine.
If you are using log4Net, you can do what you asked:
Although it is possible to add your log4net configuration settings to your project’s app.config
or web.config
file, it is preferable to place them in a separate configuration file. Aside from the obvious benefit of maintainability, it has the added benefit that log4net can place a FileSystemWatcher
object on your config file to monitor when it changes and update its settings dynamically.
To use a separate config file, add a file named Log4Net.config to your project and add the following attribute to your AssemblyInfo.cs file:
[assembly: log4net.Config.XmlConfigurator(ConfigFile="Log4Net.config", Watch = true)]
Note: for web applications, this assumes Log4Net.config
resides in the web root. Ensure the log4net.config
file is marked as “Copy To Output” -> “Copy Always” in Properties.
Following is the hack which you can put this will make config to read from disk.
You just need to save the config file in Modified Mode and then refresh this will make application to read the file agian from the disk.
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
Just a note, in WinForms, you can make programmatic changes to your app.config before your application loads (before Application.Start(new Form1())
), as long as you use System.Xml
instead of System.Configuration.ConfigurationManager
string configFile = Application.ExecutablePath + ".config"; //c:\path\exename.exe.config
XmlDocument xdoc = new XmlDocument();
xdoc.Load(configFile);
XmlNode node = xdoc.SelectSingleNode("/configuration/appSettings/add[@key='nodeToChange']/@value");
node.Value = "new value";
File.WriteAllText(setFile, xdoc.InnerXml);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With