CustomTraceListener Question

C

CGuy

Hi,

I have a question. I have implemented a custom trace listener (derived from
the TextWriterTraceListener type).
Now, I add this custom trace listener to the list of listeners and then call
a Trace.Indent() before I write to the trace. But the file produced by my
CustomTraceListener does not indent the text. How do I do this?

CGuy
 
N

Noah Coad [MVP .NET/C#]

The TraceListener class has an "IndentLevel" property that you can access to
obtain the level of indenting you should use. Here is an example I wrote
for you:

class Class1
{
public Class1(string[] args)
{
System.Diagnostics.Debug.Listeners.Clear();
System.Diagnostics.Debug.Listeners.Add(new DateStampTraceLisener());
Debug.WriteLine("Testing");
Debug.Indent();
Debug.WriteLine("Testing Indent");
}

#region Application Entry Point
[STAThread]
static void Main(string[] args)
{
new Class1(args);
Console.WriteLine("\nPress [ENTER] to exit...");
Console.ReadLine();
}
#endregion
}

class DateStampTraceLisener : TraceListener
{
public override void Write(string info)
{ Console.WriteLine("".PadLeft(IndentLevel * 2, ' ') + DateTime.Now + "
" + info); }

public override void WriteLine(string info)
{ Write(info + "\n"); }
}
 

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