Creating TraceListeners in .NET
by Michael Taylor

Example 1: 

<configuration>
     <system.diagnostics>
          <trace autoflush="false" indentsize="4">
               <listeners>
                    <add name="listenername" type="listenertype"/>
               </listeners>
          </trace>
     </system.diagnostics>
</configuration>


Listing One
//Define a delegate for the invoke routine - match the delegate signature to
//the actual method we want to call so we can use a simple "if" statement to
//differentiate the logic. 
delegate void AppendControlTextDelegate ( TextBoxBase box, string message );

//This method is called by both the UI and non-UI threads.
private void AppendControlText ( TextBoxBase box, string message )
{
   //If we are not running on the UI thread
   if (box.InvokeRequired)
   {
      //Create the delegate to do the work; pass it this method as a parameter
      AppendControlTextDelegate del = new AppendControlTextDelegate(
                                          AppendControlText);
      //Invoke the delegate with the parameters we received in this call
      box.FindForm().BeginInvoke(del, new object[] {box, message});
   } else
      //We are on the UI thread so we can use the control directly
      box.AppendText(message);
   }
}


Listing Two

//Write the given object.
public override void Write ( object obj )
{           
   Write(obj.ToString());
}
//Write the specified message.
public override void Write ( string message )
{
   if (m_Owner == null)
      return;
   //Send it to the control
   AppendControlText(m_Owner, AppendIndent(message));
}
//Write the given object using the specified category.
public override void Write ( object obj, string category )
{
   Write(obj.ToString(), category);
}
//Write the given message with the specified category.
public override void Write ( string message, string category )
{
   if (m_Owner == null)
      return;
   //Format the text
   StringBuilder bldr = new StringBuilder();
   bldr.AppendFormat("{0}{1}: {2}", GetIndentString(), category, message);
   //Send it to the control
   AppendControlText(m_Owner, bldr.ToString());
}
//All these versions are the same as Write() but also append a newline.
public override void WriteLine ( object obj )
{
   WriteLine(obj.ToString());
}
public override void WriteLine ( string message )
{
   if (m_Owner == null)
      return;
   //Append the newline
   StringBuilder bldr = new StringBuilder();
   bldr.AppendFormat("{0}{1}\r\n", GetIndentString(), message);
   //Send it to the control         
   AppendControlText(m_Owner, bldr.ToString());
}
public override void WriteLine ( object obj, string category )
{
   WriteLine(obj.ToString(), category);
}
public override void WriteLine ( string message, string category )
{
   if (m_Owner == null)
      return;
   //Format the text
   StringBuilder bldr = new StringBuilder();
   bldr.AppendFormat("{0}{1}: {2}\r\n", GetIndentString(), category, message);
   //Send it to the control
   AppendControlText(m_Owner, bldr.ToString());
}

Listing Three

//By this time the controls are all created and initialized but
//the form hasn't been displayed yet.
protected override void OnLoad(EventArgs e)
{
   base.OnLoad (e);
   //Add the textbox listener
   Trace.Listeners.Add(new TextBoxTraceListener(m_rtxtLog)); 
   //For the treeview listener associate the categories with icons
   TreeViewTraceListener lstnr = new TreeViewTraceListener(m_treeLog);
   lstnr.SetCategory("", 0);
   lstnr.SetCategory("Warning", 1);
   lstnr.SetCategory("Error", 2);
   //Now add the listener
   Trace.Listeners.Add(lstnr);         
}








3


