To implement tracing in C# application it is suggested to use class Trace.
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));VS Output window:
TraceSwitch traceSwitch = new TraceSwitch("TraceLevelSwitch", "trace switch");
Trace.WriteLine("sample_Trace.Start");
for (int i = 0; i < 5; i++)
{
Trace.Indent();
Trace.WriteLineIf(traceSwitch.TraceInfo, "index: " + i + " indentLevel: " + Trace.IndentLevel);
Trace.Unindent();
}
Trace.WriteLine("sample_Trace.Finish");
Console screen:
Trace.Listeners allows to add additional trace target (in the sample, Console).
TraceSwitch helps to implement different levels of tracing.
When value of "TraceLevelSwitch" will be set "4" (see App.config file), then output will be next:
App.config file:
VS Output window:
Console screen:
Notes:
In Visual Studio 2005 projects, by default, the "DEBUG" conditional compilation symbol is defined for debug builds, and the "TRACE" symbol is defined for both debug and release builds. For information on how to disable this behavior, see the Visual Studio 2005 documentation.Reference:
No comments:
Post a Comment