Initializing a TraceListener Writer to a stream in app.config.

M

michael sorens

I would like to be able to define a TraceListener in my app.config file whose
Writer is a stream. I understand how to do this in code, but I have not been
successful in getting it to work with the app.config file. I had hoped that I
could just omit the initializeData attribute and then assign a Writer in
code, which would suit my purpose. But it seems that any attempt to access
the TraceSource configuration generates a ConfigurationErrorsException. That
is, if I attempt to assign a Writer like this:
((TextWriterTraceListener)mySource.Listeners["ExperimentalListener"]).Writer = strWriter = new StringWriter();

I get the aforementioned exception.

Is there a way to initialize the Writer to a StringWriter within the
app.config file itself?

[My environment: .NET 2.0, VS2005]
 
S

Steven Cheng

Hi Michael,

As for the TextTraceListener, based on my research, the undelying Writer it
used is hard coded as "StreamWriter" and use the "initliazeData" to get the
log file path. Therefore, I'm afraid this class is not supposed to let you
change different kind of underlying writer. For such scenario,
implementing a custom Tracelistener should be the reasonable approach:

#Implementing a Custom TraceListener
http://www.codeguru.com/vb/gen/vb_misc/debuggingandtracing/article.php/c5611
/

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= <[email protected]>
Subject: Initializing a TraceListener Writer to a stream in app.config.
Date: Mon, 18 Feb 2008 15:36:00 -0800
I would like to be able to define a TraceListener in my app.config file whose
Writer is a stream. I understand how to do this in code, but I have not been
successful in getting it to work with the app.config file. I had hoped that I
could just omit the initializeData attribute and then assign a Writer in
code, which would suit my purpose. But it seems that any attempt to access
the TraceSource configuration generates a ConfigurationErrorsException. That
is, if I attempt to assign a Writer like this:

((TextWriterTraceListener)mySource.Listeners["ExperimentalListener"]).Writer
= strWriter = new StringWriter();
I get the aforementioned exception.

Is there a way to initialize the Writer to a StringWriter within the
app.config file itself?

[My environment: .NET 2.0, VS2005]
 
M

michael sorens

I thought that might be the case. I found, however, that if I specify a dummy
filename in the app.config, then during my program initialization I could
re-assign the Writer to be a StringWriter.
 
S

Steven Cheng

Thanks for your reply Michael,

Sounds like a workable trick. Anyway, glad that you've got it working.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
From: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= <[email protected]>
References: <[email protected]>
 

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