Use of Listeners in .NET Framework with C#

Both Debug and Trace are dependent on Listeners and both of them have Listener collections. Listener takes output from the Debug and Trace and outputs into Output Window.

ConsoleTraceListener is used to write trace and debug messages to the console for a specific section of code

Listener collection has several objects that help it direct output. They are:

  • DefaultTraceListener it is attached to Output window. We can add this Listener in two ways from within the code and config file

    Trace.Listeners.Clear();
    DefaultTraceListener MyListener = new DefaultTraceListener();
    Trace.Listeners.Add(MyListener);
    Trace.WriteLine("This test");

    Config file

     

       <configuration>

          <system.diagnostics>

                <traceautoflush="true"indentsize="5" />

          </system.diagnostics>

    </configuration>

     

  • TextWriterTraceListener directs output to a text file or a stream. It can be enabled via code of configuration file.

    Trace.Listeners.Clear();
    TextWriterTraceListener MyListener = new extWriterTraceListener(@"C:\outputfile.txt");
    Trace.Listeners.Add(MyListener);
    Trace.AutoFlush = true;
    Trace.WriteLine("Test");

    Config File

     

      <configuration>

          <system.diagnostics>

                <traceautoflush="true"indentsize="5">

                      <listeners>

                            <addname="DemoListener"type="System.Diagnostics.TextWriterTraceListener"

                            initializeData="output.txt" />

                            <removename="Default" />

                      </listeners>

                </trace>

          </system.diagnostics>

    </configuration>

  • XmlWriterTraceListener directs output to TextWriter or Stream object. Attributes of XmlWriterTraceListener

     

    NameDescription
    CallStack Will display the runtime's call stack
    Computer The name of the computer
    Correlation The Correlation object
    DataItem Value contained in the data parameter
    EventId The event ID
    Execution The execution type
    Level Any integer value from 0 through 255
    Message The specific text message
    Source The initiator of the message
    TimeCreated The current time by default
    TimeStamp The system time on the machine
    Type The object's type

    Trace.Listeners.Clear();
    XmlWriterTraceListener MyListener = new XmlWriterTraceListener (@"C:\output.xml");
    Trace.Listeners.Add(MyListener);
    Trace.AutoFlush = true;
    Trace.WriteLine("This is a test");

    Config File

     

    <?xmlversion="1.0"encoding="utf-8" ?>

    <configuration>

          <system.diagnostics>

                <traceautoflush="true"indentsize="5">

                      <listeners>

                            <addname="DemoListener"type="System.Diagnostics.XmlWriterTraceListener"

                             initializeData="output.xml" />

                            <removename="Default" />

                      </listeners>

                </trace>

          </system.diagnostics>

    </configuration>

  • EventLogTraceListener – outputs to Event Log.

    Trace.Listeners.Clear();
    DelimitedListTraceListener MyListener = new DelimitedListTraceListener (@"C:\output.txt");
    Trace.Listeners.Add(MyListener);
    Trace.AutoFlush = true;
    Trace.WriteLine("This is a test");

     

  • DelimitedListTraceListener – it work just like TextWriter but it takes delimited token and uses this token to create delimited file.

    Trace.Listeners.Clear();
    DelimitedListTraceListener MyListener = new DelimitedListTraceListener (@"C:\output.txt");
    Trace.Listeners.Add(MyListener);
    Trace.AutoFlush = true;
    Trace.WriteLine("This is a test");

    Config File

     

    <configuration>

          <system.diagnostics>

                <traceautoflush="true"indentsize="5">

                      <listeners>

                            <addname="DemoListener"

                              type="System.Diagnostics.DelimitedListTraceListener"

                              delimiter="|"

                              initializeData="output.txt"/>

                            <removename="Default" />

                      </listeners>

                </trace>

          </system.diagnostics>

    </configuration>

  • CorrelationManager – keep request isolated and unique