App.config updates

  • Thread starter Peter Larsen [CPH]
  • Start date
P

Peter Larsen [CPH]

Hi,

How to i update a TraceSwitch from app.config in runtime - without
restarting the application.

I use dotNet 3.5.

I know how to refresh the section in memory, but i dont know how to read the
properties from the updated section.
This is what i have:

System.Configuration.ConfigurationManager.RefreshSection("system.diagnostics");
object section =
System.Configuration.ConfigurationManager.GetSection("system.diagnostics");
TraceSwitch fileTraceLevel = new TraceSwitch("FileTraceListenerSwitch",
"explanation...");
or
fileTraeLevel.Level = (TraceLevel)section.??? !!

How do i get the level information from the section ??

Thank you in advance.
BR
Peter
 
C

Colbert Zhou [MSFT]

Hello Peter,

As the MSDN documentation says,
(http://msdn.microsoft.com/en-us/library/system.diagnostics.traceswitch.aspx
)
"In your application, you can use the configured switch level by creating a
TraceSwitch with the same name"

So, we can just create a new instance of TraceSwitch that has the same name
as the one in configuration file to access the configured switch. For
example, I have the following part in the app.config,
------------------------------------------------
<system.diagnostics>
<switches>
<add name="mySwitch" value="1" />
</switches>
</system.diagnostics>
------------------------------------------------

Then in the application, to get the TraceSwitch level, I just need to use
the following codes,
------------------------------------------------
private static TraceSwitch appSwitch = new
TraceSwitch("mySwitch","Switch in config file");

static void Main(string[] args)
{
Console.WriteLine("Trace switch {0} configured as
{1}",appSwitch.DisplayName, appSwitch.Level.ToString());
}
------------------------------------------------

I have tested the above codes and it works fine.


Best regards,
Ji Zhou
Microsoft Managed MSDN Newsgroup Support Team
 
P

Peter Larsen [CPH]

Hi Colbert,

Thank you for your comment.

But, if you changed the level in a app.config, belonging to a running
application, the application does not see the change until the app is
restarted.
Isn't that right ??

BR
Peter
 
C

Colbert Zhou [MSFT]

Hello Peter,

The new app.Config file is loaded after the RefreshSection calling. It is
just the Trace data does not update. We can call Trace.Refresh to update it.

static void Main(string[] args)
{
TraceSwitch appSwitch = new TraceSwitch("mySwitch", "Switch in
config file");
Console.WriteLine("Trace switch {0} configured as {1}",
appSwitch.DisplayName, appSwitch.Level.ToString());
System.Diagnostics.Debugger.Break();

System.Configuration.ConfigurationManager.RefreshSection("system.diagnostics
");
System.Diagnostics.Trace.Refresh();
Console.WriteLine("Trace switch {0} configured as
{1}",appSwitch.DisplayName, appSwitch.Level.ToString());
}

Please note if you are debugging the codes, the config to modify is
***.vshost.exe.config.


Please give it a try and let me know it works for you!


Best regards,
Ji Zhou
Microsoft Newsgroup Support Team
 
P

Peter Larsen [CPH]

Hi Colbert,

Thanks for the reply.
I will look into this monday/tuesday next week where i'm going to work on
the project where i have this problem.

BR
Peter
 
P

Peter Larsen [CPH]

Hi Colbert,

I have tried what you suggested and it works.
I think i have missed the refresh method on Trace.

Why is it that "appSettings" is available after RefreshSection() and trace
isn't ??

BR
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top