Notification of App.config changes when Application is running

Topics: General discussion, Logging Application Block
Jan 23, 2009 at 3:05 PM

Is there any built in mechanism provided by Ent Lib that would give notifications changes of App.config ?

 

I am supposed to reload changes of App.conig, when application is still running.

 

Example : If someone changes “file name” in the below config section, how would Ent lib detect/notify to the consumer application about the changes?

 

<add fileName="D:\Output\Logs\FlatFile.log" header="----------------------------------------"/>

Any code snippets or links would be of great help.

Thanks
Pavan

Jan 24, 2009 at 4:19 AM
Any help on this ? Still looking for reply
Jan 24, 2009 at 2:52 PM
Hi,

Please see this if this helps:
http://msdn.microsoft.com/en-us/library/cc511916.aspx

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Jan 24, 2009 at 3:55 PM
I understand that, this logic would work when reading external config files.

My question was specific to App.config and how do we re-load changes done to it .

Thanks,
Pavan
Jan 26, 2009 at 9:05 AM
Hi,

Here is a sample code, that i've tested in a console application, the code simply adds a sectionchangehandler, upon runtime, i edited the path of the Flatfile trace listener to pointing to other directory, then it was detected and i also do a log to the new directory that i've set at runtime. I've use this as my guide: http://www.codeplex.com/entlib/Thread/View.aspx?ThreadId=10757


static

void Main(string[] args)
{
SystemConfigurationSource source = new SystemConfigurationSource();
source.AddSectionChangeHandler(
LoggingSettings.SectionName, Changed);
LoggingSettings settings = source.GetSection(LoggingSettings.SectionName) as LoggingSettings;
if (settings != null)
{
FlatFileTraceListenerData traceListenerData = settings.TraceListeners.Get("FlatFile TraceListener") as FlatFileTraceListenerData;
if (traceListenerData != null)
{
System.
Console.WriteLine("Current Path: " + traceListenerData.FileName);
}
}
System.
Console.WriteLine("Press enter to stop");
System.
Console.ReadLine();
}
static void Changed(object sender, ConfigurationChangedEventArgs e)
{
System.
Console.WriteLine(string.Format("Changed! section '{0}'", e.SectionName));
SystemConfigurationSource source = new SystemConfigurationSource();
source.GetSection(
LoggingSettings.SectionName);
source.AddSectionChangeHandler(
LoggingSettings.SectionName, Changed);
LoggingSettings settings = source.GetSection(LoggingSettings.SectionName) as LoggingSettings;
if (settings != null)
{
FlatFileTraceListenerData traceListenerData = settings.TraceListeners.Get("FlatFile TraceListener") as FlatFileTraceListenerData;
if (traceListenerData != null)
{
System.
Console.WriteLine("New Path: " + traceListenerData.FileName);
LogEntry entry = new LogEntry();
entry.Message =
"Logging to the new path...";
entry.Categories.Add(
"General");
Logger.Write(entry);
}
}
}


Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com