Trace & Config Files???

G

Guest

Hi,

I'm having trouble setting up a config file based trace switch & listener. I added an application config file to my console based C# program using the "Add New Application Config File" button from solution explorer. My config file is composed of the following, which is almost a verbatum cut & paste from MSDN help:

---------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<switches>
<add name="mySwitch" value="3" />
</switches>
<trace autoflush="true" indentsize="2">
<listeners>
<add name="myListener"
type="System.Diagnostics.TextWriterTraceListener, Version, Culture, PublicKeyToken"
initializeData="MyListener.log"/>
</listeners>
</trace>
</system.diagnostics>
</configuration>
---------------------------------------------------------------------------

As for code I followed the MSDN help for the switch, but I'm not certain of what to do for the Listener. I coded the following:

---------------------------------------------------------------------------
class Class1
{
static TraceSwitch mySwitch = new TraceSwitch("","");
static TextWriterTraceListener myListener = new TextWriterTraceListener();

[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Trace Level == {0}", mySwitch.Level);

Trace.WriteLine("Hello config file based Trace");
}
}
---------------------------------------------------------------------------

When I run this program 2 rotten things happen:

1) "mySwitch.Level" returns "Off", which is not correct; it was supposed to be set to level 3 as specified in the config file.

2) I get a ConfigurationException when I execute the Trace statement. The error message reads "Couldn't find type for class TextWriterTraceListener".

Does anybody have any ideas on how to make this stuff work? MSDN help is useless in this area as everything is in piecemeal snippets with no complete working example...

Also, I get the feeling that I shouldn't have to create a listener in code, it seems to me that the listener could be created behind the scenes by the framework - yes? Do I need to have any code for the listener?

Any help would be greatly appreciated...

--Richard
 
G

Guest

I have a partial answer to my own question:

YES the MSDN documentation is bogus. In one example the docs use a config file switch named "mySwitch" and in another example they use a config file switch named "General". The example code seems to criss cross between the two names and it never puts the correct config file switch name together with the correct name parameter as it must appear in source code. Aka the "name" parameter of the C# switch class constructor in code must match the name of the switch as it appears in the config file:

---------------------------------------------------------------------------
<switches>
<add name="mySwitch" value="3" />
</switches>
---------------------------------------------------------------------------
class Class1
{
static TraceSwitch mySwitch = new TraceSwitch("mySwitch","");
}
---------------------------------------------------------------------------



Richard said:
Hi,

I'm having trouble setting up a config file based trace switch & listener. I added an application config file to my console based C# program using the "Add New Application Config File" button from solution explorer. My config file is composed of the following, which is almost a verbatum cut & paste from MSDN help:

---------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<switches>
<add name="mySwitch" value="3" />
</switches>
<trace autoflush="true" indentsize="2">
<listeners>
<add name="myListener"
type="System.Diagnostics.TextWriterTraceListener, Version, Culture, PublicKeyToken"
initializeData="MyListener.log"/>
</listeners>
</trace>
</system.diagnostics>
</configuration>
---------------------------------------------------------------------------

As for code I followed the MSDN help for the switch, but I'm not certain of what to do for the Listener. I coded the following:

---------------------------------------------------------------------------
class Class1
{
static TraceSwitch mySwitch = new TraceSwitch("","");
static TextWriterTraceListener myListener = new TextWriterTraceListener();

[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Trace Level == {0}", mySwitch.Level);

Trace.WriteLine("Hello config file based Trace");
}
}
---------------------------------------------------------------------------

When I run this program 2 rotten things happen:

1) "mySwitch.Level" returns "Off", which is not correct; it was supposed to be set to level 3 as specified in the config file.

2) I get a ConfigurationException when I execute the Trace statement. The error message reads "Couldn't find type for class TextWriterTraceListener".

Does anybody have any ideas on how to make this stuff work? MSDN help is useless in this area as everything is in piecemeal snippets with no complete working example...

Also, I get the feeling that I shouldn't have to create a listener in code, it seems to me that the listener could be created behind the scenes by the framework - yes? Do I need to have any code for the listener?

Any help would be greatly appreciated...

--Richard
 
D

Daniel Billingsley

So do you have it working? I wrestled with this quite a while a few months
ago. As you say, the doc is questionable.
 

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