views:

3012

answers:

8

How do I dynamically reload the app.config in a .net Windows application? I need to turn logging on and off dynamically and not just based upon the value at application start.

ConfigurationManager.RefreshSection("appSettings") does not work and I've also tried explicitly opening the config file using OpenExeConfiguration but I always get the cached value at application startup and not the current value.

I've accepted the answer of creating a custom configuration section. As a side note and foolish mistake - if you're running from the IDE there's no point in updating the app.config file and expecting changes. Yuo have to modify the .exe.config file in the bin\debug folder. Doh!

A: 

I don't think there's any way to do this, unless you write your own config file reader using XML. Why not just turn logging on or off at the start of your app based on the config file setting, and then just turn it on or off dynamically while the program is running?

MusiGenesis
A: 

I think I read in the log4net documentation that this is not possible.

Try having the logging in an external logfile that can be watched with a filesystemwatcher

Update: Found it again.. http://logging.apache.org/log4net/release/manual/configuration.html#.config%20Files

There is no way to reload the app.config during runtime.

Tigraine
log4net doesn't allow dynamically turning logging on or off?
MusiGenesis
Log4net allows this. But if your log4net config is set in your application.config then there is no way to change that configuration during runtime.
Tigraine
A: 

I would recommend using another XML file and not app.config. You could even watch the file for changes and automatically reload it when it changes.

+6  A: 

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.

Daok
Here's a great article on how to do exactly thathttp://devlicio.us/blogs/derik_whittaker/archive/2006/11/13/app-config-and-custom-configuration-sections.aspx
AlexDrenea
+2  A: 

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.

Mitch Wheat
+1  A: 

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);
foson
A: 

Actually using an:

Application.restart();

Has worked pretty good for me.

Regards

Jorge

Application.restart() won't work if you have variables that need to retain their state. Also, I ended up spawning a new window with a delay of around 10-15 seconds of closing the old one, so this certainly is not an ideal solution.
adam_0
+1  A: 

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

Ruchir
Has anyone else here tried this?
Rice Flour Cookies