When utilizing Trace, Debug and TraceSource, you need to have a mechanism for recording and collecting the messages which are sent. Trace messages are obtained by listeners. The objective of a listener would be to accumulate, store, and route tracing messages. Listeners direct the tracing output into an proper goal, like a window, log, or text document.

Listeners are readily available into the Debug, Trace, and TraceSource classes, all of which may send its own output to many different listener objects. These are the commonly used artists that are predefined:

A TextWriterTraceListener redirects output to a case of this TextWriter course or to anything That’s a Stream course. Additionally, it may write to the console or to a document, because these really are Stream classes.

A DefaultTraceListener emits Write and WriteLine messages into the OutputDebugString and into the Debugger.Log Technique. In Visual Studio, this also results in the debugging messages to show up in the Output window. Fail and neglected Assert messages additionally emit into the OutputDebugString Windows API along with also the Debugger.Log method, and cause a message box to be displayed. This behaviour is the default behaviour for Debug and Trace messages, since DefaultTraceListener is automatically included in each Listeners set and is the sole speaker mechanically included.

A ConsoleTraceListener directs tracing or debugging output into the output or the standard error stream.

A DelimitedListTraceListener directs tracing or debugging output to a text author, including a flow author, or into a flow, including a document stream. The input is in a delimited text format which employs the delimiter specified by the Delimiter real estate.

An XmlWriterTraceListener directs tracing or debugging output as XML-encoded information to some TextWriter or into some Stream, like a FileStream.

If you’d like any listener aside from the DefaultTraceListener to get Debug, Trace and TraceSource outputsignal, you have to add it into the Listeners collection. Any listener from the Listeners collection receives the very same messages in the trace output procedures. By way of instance, suppose you put up two listeners: an TextWriterTraceListener along with also an EventLogTraceListener. Each listener gets the exact same message. The TextWriterTraceListener will direct its output to a flow, and also the EventLogTraceListener would guide its output into a event log.

The next example demonstrates how to send output to the Listeners collection.

// Use this case when debugging.

Debug and follow share exactly the identical Listeners set, so in case you add a listener object to some Debug.Listeners set on your program, it has added into the Trace.Listeners set also.

The next example demonstrates how to use a listener to ship tracing data to a console:

System.Diagnostics.Trace.Listeners.Add(
brand new System.Diagnostics.TextWriterTraceListener(Console.Out));
Developer-Defined Listeners

You are able to set your own listeners by inheriting in your TraceListener base class and overriding its approaches with your customized approaches. To learn more on producing developer-defined listeners, visit TraceListener from the .NET Framework reference.