473,503 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3679
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3800
by: Sean Walker | last post by:
I have installed the Microsoft Enterprise Instrumentation framework and have installed the samples. I have events successfully being logged to the Application Event Log using the Simple...
8
3730
by: Geopsaros | last post by:
Hi! I have created a custom trace Listener class, called "DBTraceListener" it works fine when i add it manually in code : (eg. Trace.listeners.add(new DBTraceListener("myDBListener",...
3
2593
by: Joshua Coady | last post by:
Do Trace calls have any impact on performance if Trace is disabled in the config file? Josh
4
1862
by: António Pinho | last post by:
Hi, I have a big problem with an webpart/assembly. i'm trying to connect to sql server but i get the error "Request for the permission of type System.Data.SqlClient.SqlClientPermission,...
5
2910
by: who be dat? | last post by:
Hello all. I'm writing an application that is writing trace information that can be viewed in trace.axd. I would like to rename this and use a different name specific to my application. I know...
9
4376
by: Joe Rattz | last post by:
I can't seem to get to trace.axd. I have turned tracing on in web.config. Here is how I currently have i configured: <trace enabled="true" requestLimit="10" pageOutput="false"...
0
3158
by: Felix | last post by:
Ok, I've had this issues since yesterday and I cannot figure out what's causing it. Basically, I have a Visual Studio 2005 Web Service. The service runs completely find on my local development...
5
16522
by: MJB | last post by:
I have several dlls that have app.config files tied to them. When adding their primary output to a Setup project everything works fine, but only the app.config for the .exe gets added to the Setup...
17
2822
by: Arpan | last post by:
When a Button is clicked in a Web Form in an ASPX page, the Form will post back to itself. Under such circumstances (i.e. when a Button is clicked), will the Page_Load sub execute first & then will...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.