Can Collection/Array be used as a Trace/Debug Listener?

  • Thread starter Thread starter llyal2000
  • Start date Start date
L

llyal2000

I am a C# newbie. In VB i had developed an infrastructure
that allowed recent trace/debug information to be kept in
memory. I would like to do similar in .Net. Is it
possible? or will i have to write my own code? From what i
have read in the Microsoft books and documentation it
seems that a Trace Listener can only be a stream, like
writing to a disk file. But i don't want to capture all
trace events to an external file or db, but store only
some trace events in memory so to be available to an error
handler. Example, application will retain the last 100
trace events in memory; when exception occurs, central
error handler dumps trace information and creates error
report. Can this scenario work with the Trace/debug
classes? What is the How To?

Thank you
 
llyal2000 said:
I am a C# newbie. In VB i had developed an infrastructure
that allowed recent trace/debug information to be kept in
memory. I would like to do similar in .Net. Is it
possible? or will i have to write my own code? From what i
have read in the Microsoft books and documentation it
seems that a Trace Listener can only be a stream, like
writing to a disk file. But i don't want to capture all
trace events to an external file or db, but store only
some trace events in memory so to be available to an error
handler. Example, application will retain the last 100
trace events in memory; when exception occurs, central
error handler dumps trace information and creates error
report. Can this scenario work with the Trace/debug
classes? What is the How To?

using System.Diagnostics;
using System.IO;
.. . .

//hold on to a reference to this object
StringWriter writer = new StringWriter();

//hold on to a reference to this object too if you want to remove the trace
listener
TextWriterTraceListener tl = new TextWriterTraceListener(writer);

Trace.Listeners.Add(tl);
Trace.WriteLine("Starting to write to the trace.");
//do whatever
Trace.Listeners.Remove(tl);

//now all the trace output is stored in the StringWriter's StringBuilder
Console.WriteLine(writer.GetStringBuilder().ToString());

David
 
llyal2000 said:
David,

That's an idea- resolves the in-memory requirement. I wonder if it will
allow me to remove old trace entries as new entries are added? I will
look into. If not, then i will write my own code. It is starting to look
like the Trace/Debug classes only work with stream objects and cannot
work with collections or arrays. Since collections and arrays are finite
in storage size, it requires management to keep then from over-flowing-
as new entries are added, old entries must be removed.

You can derive your own class from TraceListener and handle the entries
however you like.
 
Thanks John,

David's response started me thinking that there might be more Trace
Listener possibilities than what comes "stock" with Net. After more
searching I found some How-To articles on making your own Listener. It
looks pretty straightforward. You inherit the Listener class into your
own and then define a few methods to handle the trace data. I think i am
pointed in the right direction now.

--Llyal




*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Back
Top