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