Thursday, February 01, 2007

* Debugging and Profiling .NET Applications

Debugging or Troubleshooting an application in production environment can become nightmare if the application has not been instrumented well.

The .NET framework namespace System.Diagnostics provides following classes : Trace, Debug, and TraceSource for tracing and debugging applications.

The Trace and Debug classes ae identical in the functionality they provide except that Trace is part of Release build by default while Debug statements are removed from Release build.

Before seeing Tracing into action lets understand some important components of Tracing framework.

  • Listeners : Trace output is collected by objects called listeners which redirect it to the configured device like console, flat file etc.
  • Switches : Switches allow to enable, disable or filter tracing outputs. These can be configured through the .config file. There are basically three types of switches BooleanSwitch, TraceSwitch and SourceSwitch. One of the ways to filter trace output is TraceLevel where it can have values from 0 to 4. 0 means Off and 4 means Verbose Messages.

So all the tracing data after getting filtered using switches goes to listeners which redirect them to configured output.

Lets look at an example now :

 

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Text;
   4:  using System.Diagnostics;
   5:   
   6:  namespace TracingSample
   7:  {
   8:      class Program
   9:      {
  10:          static void Main(string[] args)
  11:          {
  12:              TraceSource ts = new TraceSource("TracingSampleSource");
  13:              ts.TraceEvent(TraceEventType.Error,1,"Error message");
  14:              ts.TraceEvent(TraceEventType.Information,2,"Info Message");
  15:              ts.Flush();
  16:              ts.Close();
  17:          }
  18:      }
  19:  }






The config file for this program looks like this :



 

<configuration>
<system.diagnostics>
<sources>
<source name="TracingSampleSource"
switchName="sourceSwitch"
switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="myListener"/>
<add name="console" type="System.Diagnostics.ConsoleTraceListener" />

</listeners>
</source>
</sources>
<switches>
<add name="sourceSwitch" value="Information"/>
</switches>
<sharedListeners>
<add name="myListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="myListener.log">
<filter type="System.Diagnostics.EventTypeFilter"
initializeData="Error"/>
</add>
</sharedListeners>
</system.diagnostics>
</configuration>






 



The code usage is fairly simple, create an instance of TraceSource, logged two events using it where one is of Error type while other of Info type.



Its the configuration ability which makes this framework so powerful. The source has been configured to send messages to two listeners 'myListener' which redirects the messages to a log file called myListener.log and console. At source level the messages have been filtered using sourceSwitch switch while at listener level it has been configured for Error messages only.



The outputs of the two listeners are as follows :



Log File



TracingSampleSource Error: 1 : Error message



Console output :



TracingSampleSource Error: 1 : Error message
TracingSampleSource Information: 2 : Info Message



Other Posts



 

No comments:

Post a Comment