How To: Configuration File for your Application

G

Guest

Hello,

Thanks for reviewing my question. I am trying to understand how to use a
configuration file for my application, so I can turn debug or trace with a
switch. The following is what I put together from my understanding but
doesn't work.

AssemblyInfo.cs
------------------
[assembly: AssemblyConfiguration("RegConsole.xml")]

RegConsole.xml
------------------
<configuration>
<system.diagnostics>
<switches>
<add name="mySwitch" value="4" />
</switches>
</system.diagnostics>
</configuration>


RegConsole.exe
------------------
static TraceSwitch mySwitch = new TraceSwitch("General", "Entire
Application");

static public void Main()
{
if(mySwitch.TraceError)
Console.WriteLine("My error message.");

if(mySwitch.TraceVerbose)
Console.WriteLine("My second error message.");
}

Many Thanks
Peter
 
G

Guest

Hi,

I had trouble with this a while back - I found the MSDN documentation to be
a bit flaky and inconsistent for examples of the trace facility stuff.
Anyway, I got it to work and here are snippets from a working App.config file
and TraceSwitch instantiation code {note that the 'name' attribute in the
config file must match the 'name' parameter in the TraceSwitch constructor}:

----App.config
-------------------------------------------------------------------

<system.diagnostics>
<switches>
<add name="ServiceSwitch" value="2" />
<add name="XmlDocumentSwitch" value="2" />
</switches>
<trace autoflush="true" indentsize="2">
<listeners>
<add name="AtlasLogProcessor.log"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\Logs\LogProcessor.log" />
</listeners>
</trace>
</system.diagnostics>

---- Source code
----------------------------------------------------------------

TraceSwitch ts = new TraceSwitch("ServiceSwitch", "");
......

Trace.WriteLineIf(ts.TraceError, "Error");

------------------------------------------------------------------------------------

--Richard

P.S. The documentation states that changes to the config file trace switch
levels at runtime will be reflected immediately in the running program. I
have not seen this work as advertised on Server 2003. I opened an MSDN
ticket on this issue but Microsoft never got back to me {months ago}...
 
G

Guest

Oops, make that construction call:

static TraceSwitch ts = new TraceSwitch("ServiceSwitch", "");

As per the documentation my TraceSwitch instance is static as they recommend
- actually in one place I think they tell you it MUST be static and in
another place they say that it can be static to improve performance - dunno
what's up with that but static was good for my needs so I made it static...

Also I don't have anything particular in my AssemblyInfo.cs file related to
my TraceSwitch settings, here's my pertinent assembly info:

[assembly: AssemblyTitle("LogProcessor")]
[assembly: AssemblyConfiguration("")]
..... {etc. nothing special for TraceSwitch's }

--Richard

Richard said:
Hi,

I had trouble with this a while back - I found the MSDN documentation to be
a bit flaky and inconsistent for examples of the trace facility stuff.
Anyway, I got it to work and here are snippets from a working App.config file
and TraceSwitch instantiation code {note that the 'name' attribute in the
config file must match the 'name' parameter in the TraceSwitch constructor}:

----App.config
-------------------------------------------------------------------

<system.diagnostics>
<switches>
<add name="ServiceSwitch" value="2" />
<add name="XmlDocumentSwitch" value="2" />
</switches>
<trace autoflush="true" indentsize="2">
<listeners>
<add name="AtlasLogProcessor.log"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\Logs\LogProcessor.log" />
</listeners>
</trace>
</system.diagnostics>

---- Source code
----------------------------------------------------------------

TraceSwitch ts = new TraceSwitch("ServiceSwitch", "");
.....

Trace.WriteLineIf(ts.TraceError, "Error");

------------------------------------------------------------------------------------

--Richard

P.S. The documentation states that changes to the config file trace switch
levels at runtime will be reflected immediately in the running program. I
have not seen this work as advertised on Server 2003. I opened an MSDN
ticket on this issue but Microsoft never got back to me {months ago}...

Peter said:
Hello,

Thanks for reviewing my question. I am trying to understand how to use a
configuration file for my application, so I can turn debug or trace with a
switch. The following is what I put together from my understanding but
doesn't work.

AssemblyInfo.cs
------------------
[assembly: AssemblyConfiguration("RegConsole.xml")]

RegConsole.xml
------------------
<configuration>
<system.diagnostics>
<switches>
<add name="mySwitch" value="4" />
</switches>
</system.diagnostics>
</configuration>


RegConsole.exe
------------------
static TraceSwitch mySwitch = new TraceSwitch("General", "Entire
Application");

static public void Main()
{
if(mySwitch.TraceError)
Console.WriteLine("My error message.");

if(mySwitch.TraceVerbose)
Console.WriteLine("My second error message.");
}

Many Thanks
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