469,964 Members | 1,576 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

Trace & Config Files???

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

Nov 16 '05 #1
2 3427
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" wrote:
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

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

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
I have a partial answer to my own question:

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Geopsaros | last post: by
3 posts views Thread by Joshua Coady | last post: by
4 posts views Thread by António Pinho | last post: by
9 posts views Thread by Joe Rattz | last post: by
17 posts views Thread by Arpan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.