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

Re: Can Collection/Array be used as a Trace/Debug Listener? by David

David
Thu Jun 10 17:38:00 CDT 2004


"llyal2000" <lgordonlistserv@evazeconsulting.com> wrote in message
news:1b17001c44f27$da5e0490$a001280a@phx.gbl...
> 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