PC Review


Reply
Thread Tools Rate Thread

App.config updates

 
 
Peter Larsen [CPH]
Guest
Posts: n/a
 
      9th Oct 2009
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


 
Reply With Quote
 
 
 
 
Colbert Zhou [MSFT]
Guest
Posts: n/a
 
      12th Oct 2009
Hello Peter,

As the MSDN documentation says,
(http://msdn.microsoft.com/en-us/libr...aceswitch.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

 
Reply With Quote
 
Peter Larsen [CPH]
Guest
Posts: n/a
 
      14th Oct 2009
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


"Colbert Zhou [MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello Peter,
>
> As the



 
Reply With Quote
 
Colbert Zhou [MSFT]
Guest
Posts: n/a
 
      15th Oct 2009
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

 
Reply With Quote
 
Peter Larsen [CPH]
Guest
Posts: n/a
 
      20th Oct 2009
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


"Colbert Zhou [MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello Peter,



 
Reply With Quote
 
Peter Larsen [CPH]
Guest
Posts: n/a
 
      27th Oct 2009
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



"Colbert Zhou [MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello Peter,
>
> The new app.Config file is loaded after the RefreshSection calling. It is



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ARC boot config error after updates ssitech Windows Vista General Discussion 3 10th Jan 2008 04:21 PM
DTDs for machine.config, web.config, security.config available ? =?Utf-8?B?UGF1bCBLZW5uZWR5?= Microsoft Dot NET Framework 1 5th Feb 2007 05:42 AM
app.config Updates With No-Touch =?Utf-8?B?U2FuZG1hbg==?= Microsoft Dot NET Framework Forms 0 14th Mar 2005 03:55 PM
Dynamic updates to add.config .net windows service? billym Microsoft C# .NET 0 12th Dec 2003 02:00 AM
Automatic updates not working using GPO config of windows update Jason Ede Microsoft Windows 2000 Windows Updates 0 13th Sep 2003 10:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:00 PM.