Trace Listeners question-- config file [C#]

  • Thread starter Thread starter Jim Bancroft
  • Start date Start date
J

Jim Bancroft

Hi all,

I'm experimenting with custom trace listeners, and wondered if someone
could point me in the right direction. Right now I have a listener which
writes to a text file, as outlined below:

Stream myFile = File.Create("TestFile.txt");
TextWriterTraceListener myTextListener = new
TextWriterTraceListener(myFile);
Trace.Listeners.Add(myTextListener);


The code works, but I'd like to avoid hard-coding the listener type in
my source. I'd like (ideally) to modify app.config and redirect my trace
info to the event log or (with a customized listener) a SQL table if need
be. Can someone suggest a way to do this? I've been reading up on
system.diagnostics tags but I'm not quite sure how best to go about
it....thanks a lot for pointing me in the right direction.

-Jim
 
Jim Bancroft said:
Hi all,

I'm experimenting with custom trace listeners, and wondered if someone
could point me in the right direction. Right now I have a listener which
writes to a text file, as outlined below:

Stream myFile = File.Create("TestFile.txt");
TextWriterTraceListener myTextListener = new
TextWriterTraceListener(myFile);
Trace.Listeners.Add(myTextListener);


The code works, but I'd like to avoid hard-coding the listener type in
my source. I'd like (ideally) to modify app.config and redirect my trace
info to the event log or (with a customized listener) a SQL table if need
be. Can someone suggest a way to do this? I've been reading up on
system.diagnostics tags but I'm not quite sure how best to go about
it....thanks a lot for pointing me in the right direction.

Sure,

Just add the listeners through the config file like this:

<system.diagnostics>
<trace autoflush="true" indentsize="0">
<listeners>
<add name="MyListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="MyListener.log"/>
</listeners>
</trace>
</system.diagnostics>

If you want to write your own trace listeners, just put them in your
assembly and put in the class name in the type attribute.

David
 
Back
Top