Tracer assembly 
Jeff Key
December 11, 2002
Updated: November 7, 2003

I mentioned on my .NET list a while ago that I had a useful tracer class.  Here it is.  Essentially, it gives you the text you write to the tracer, as well as the following optional fields:

  • Thread ID
  • Date/time
  • Line count
  • Elapsed time of call
  • Asterisk for highlighting lines of interest (useful to find relevant items when writing many lines)

The following shows all optional fields enabled, which you generally wouldn't do.  I usually have only the line number showing.  If I'm doing multi-threaded debugging, the thread ID is useful and when doing performance testing I turn on the execution times.

[4788] 2/1/2003 1:11:13 AM 000009              Start DesignSurface.Control_MouseDown
[4788] 2/1/2003 1:11:13 AM 000010                Start DesignSurface.SelectControl
[4788] 2/1/2003 1:11:13 AM 000011                  Location changed
[4788] 2/1/2003 1:11:13 AM 000012                  Start DesignSurface.OnControlRemoved
[4788] 2/1/2003 1:11:13 AM 000013 0027.007ms       End DesignSurface.OnControlRemoved
[4788] 2/1/2003 1:11:13 AM 000014                  Start DesignSurface.OnControlAdded
[4788] 2/1/2003 1:11:13 AM 000015 0010.336ms       End DesignSurface.OnControlAdded
[4788] 2/1/2003 1:11:13 AM 000016 0094.775ms     End DesignSurface.SelectControl
[4788] 2/1/2003 1:11:13 AM 000017 0107.176ms   End DesignSurface.Control_MouseDown
[4788] 2/1/2003 1:11:13 AM 000018              Start DesignSurface.SelectedControl_MouseMove
[4788] 2/1/2003 1:11:13 AM 000019            *   Drawing frame
[4788] 2/1/2003 1:11:13 AM 000020 0005.696ms   End DesignSurface.SelectedControl_MouseMove

Notes:

  • Tracing is disabled by default; you need to set Enabled = true.
  • When Enabled is false, all called methods are returned immediately, so you can leave the tracing calls in release code and set Enabled via config files or some other dynamic method.

Notes for version 1.2:

  • Added WriteStartMethod, which gets the name of the calling class.method and performs a WriteStart with that name.  This significantly reduces the time it takes to add trace code since you simply need to add Tracer.WriteStartMethod() at the beginning of the procedure to trace and Tracer.WriteEnd() at the end.  Note that this uses the call stack to get the caller's info, so it shouldn't be used for performance testing.
  • Made all output fields (except the written text) optional via the ShowThreadID, ShowDateTime, ShowCounter and TrackTime properties.  Note that if TrackTime is set to false, not only is the field not displayed but the time calculations are not performed, saving a few cycles.
  • Switched the time tracking code to use QueryPerformanceCounter for a bit more precision.

Notes for version 1.3:

  • Added AllowIgnoringOfTypes.

Download the assembly and help file. (21k)

Source

VB Sample Sample VB.NET project using the Tracer (11k)

Back to dotnet