for RuBoard |
Both the Debug and Trace classes have a static Listeners collection. This collection of TraceListeners represents a list of TraceListener objects that want to receive the output from the Debug or Trace class. Listeners are added to or removed from the collection just as with any other .NET collection.
TextWriterTraceListener ConsoleOutput = new TextWriterTraceListener(Console.Out, "Console.Out Listener"); Trace.Listeners.Add(ConsoleOutput); Stream OutputFile = File.Create("output.txt"); TextWriterTraceListener OutputFileListener = new TextWriterTraceListener(OutputFile, "Output File Listener"); Trace.Listeners.Add(OutputFileListener); Trace.Listeners.Remove("Default");
In this code extract, the OutputFileListener in the example will send the Trace output to a file called output.txt . The DefaultTraceListener is added automatically to the Listener collections. Any of the listeners, including the default listener, can be removed from the collection by invoking the collection's Remove method. To list all listeners in the collection:
foreach(TraceListener tr in Trace.Listeners) { Console.WriteLine("\t" + tr.Name); }
for RuBoard |