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 sta
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.
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.
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.
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);
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?
I have tried using the RefreshSection method and got it to work using the following code sample:
class Program
{
static void Main(string[] args)
{
string value = string.Empty, key = "mySetting";
Program program = new Program();
program.GetValue(program, key);
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
}
/// <summary>
/// Gets the value of the specified key from app.config file.
/// </summary>
/// <param name="program">The instance of the program.</param>
/// <param name="key">The key.</param>
private void GetValue(Program program, string key)
{
string value;
if (ConfigurationManager.AppSettings.AllKeys.Contains(key))
{
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("Key found, evaluating value...");
value = ConfigurationManager.AppSettings[key];
Console.WriteLine("Value read from app.confg for Key = {0} is {1}", key, value);
Console.WriteLine("--------------------------------------------------------------");
//// Update the value
program.UpdateAppSettings(key, "newValue");
//// Re-read from config file
value = ConfigurationManager.AppSettings[key];
Console.WriteLine("New Value read from app.confg for Key = {0} is {1}", key, value);
}
else
{
Console.WriteLine("Specified key not found in app.config");
}
}
/// <summary>
/// Updates the app settings.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public void UpdateAppSettings(string key, string value)
{
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (configuration.AppSettings.Settings.AllKeys.Contains(key))
{
configuration.AppSettings.Settings[key].Value = value;
}
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}