log4net •  C++ •  VB6 •  VBA •  .NET  •  Silverlight •  Windows Phone 7 •  ASP.NET

Universal Trace Monitor for Software Developers and Testers

Using DevTracer with .NET

The .NET framework provides in namespace System.Diagnostics all classes required for tracing: the classes Trace and Debug with methods Write, WriteLine, Indent, and Unindent .

The main difference between the classes is: Debug applies only to the debug version of the application, Trace applies to debug and release versions.

Trace and Debug are very flexible concerning the targets of the trace information. The targets of trace information are Tracelisteners. Some trace listeners are included with .NET, like the TextWriterTraceListner and the DefaultTraceListener. The later is responsible for sending trace information to the output window of Visual Studio.

DevTracer Component is a TraceListener.

The following image shows some simple code in C# and the output it generates in DevTracer Monitor.

C# example

And an example for Visual Basic.NET:

VB.NET example

DevTracer component and its TraceListener are implemented in assembly NRSoftware.Tracer.dll. During setup of DevTracer this assembly is added to the Global Assembly Cache. Therefore it is accessible to all applications.

There are three techniques to prepare an application to use DevTracer:

  • Add a reference to NRSoftware.Tracer.dll. This approach is not recommended. It requires that NRSoftware.Tracer.dll is distributed with the application, because the application will not run without this assembly. Adding a reference is not explained here, because the other options provide more flexibility.

  • Add some lines to your application’s configuration file. For ASP.NET applications the lines have to be added to WEB.CONFIG.

  • Add some code to your application which is called once when the application is started. DevTracer component's TraceListener is loaded using reflection.

Using a Configuration File

Add the following lines to your configuration file:

<configuration>
 <system.diagnostics>
  <trace autoflush="false" indentsize="4">
   <listeners>
    <add name="myListener" 
         type="NRSoftware.Tracer.Listener,NRSoftware.Tracer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=867dfab712f16c0b" 
         initializeData="localhost:12345" />
    <remove name="Default" />
   </listeners>
  </trace>
 </system.diagnostics>
</configuration>

These lines have to be added if you didn’t have a configuration file. If you already have one, the <configuration> and </configuration> tags of course must not be added again.

The value for initilizeData is very important: it specifies the DevTracer Monitor IP-address to use (in this example localhost, which is the same as 127.0.0.1) and the port number (here 12345).

The line <remove name=”Default” /> is optional. It removes the listener which sends diagnostics output to the output window of Visual Studio.

Warning: if you use your application with the configuration file on a computer which does not have DevTracer Component installed, an exception will be thrown when trace information is generated for the first time using one of the Trace or Debug methods.

The procedure is the same for ASP.NET applications. For ASP.NET add the lines to WEB.CONFIG.

The main advantage of using a configuration file is that you can use DevTracer with an existing application without touching the code, provided that the application is using
Trace or Debug to generate trace information.

Using Reflection

The listing below shows a class which loads the TraceListener of DevTracer component using reflection:

Add this class to your project. You must call TracerInit.InitListener() once in your application. If you develop a Windows Forms application, Main() is a good place.

For ASP.NET applications you can call this method in Application_Start() of the Global Application class.