Better Tracing in ASP.NET
ASPAlliance.com: The #1 ASP.NET Community
The ASPSmith
Search
D: | Domains | Authors.aspalliance.com | Stevesmith | Articles | Better Tracing in ASP.NET
Better Tracing in ASP.NET

By Steven Smith

There are three features I wish ASP.NET tracing had out of the box:

  • Check for a null context and gracefully disable if context is null (e.g. at design time)
  • If I don't specify a category, default to the name of the method in which the trace is being called.
  • Automatically compile out of production code via the Conditional("DEBUG") attribute or something similar.  Today, Trace routines carry overhead even when tracing is turned off.

To provide these features, I have a simple wrapper class.  You can name the class itself whatever you like but I recommend it be something short so that you don't have to type much to get Tracing into your code.  Feel free to use this wherever you like:

Imports System.Diagnostics

Public Class Toolkit

 

    <Conditional("DEBUG")> _

    Public Shared Sub Trace(ByVal Message As String)

        Toolkit.Trace(New System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name, _

Message)

    End Sub

 

    <Conditional("DEBUG")> _

    Public Shared Sub Trace(ByVal Category As String, ByVal Message As String)

        Toolkit.Trace(Category, Message, Nothing)

    End Sub

 

    <Conditional("DEBUG")> _

    Public Shared Sub Trace(ByVal Category As String, ByVal message As String, _

ByVal myException As Exception)

        Dim context As HttpContext = HttpContext.Current

        If Not context Is Nothing Then

            context.Trace.Write(Category, message, myException)

        End If

    End Sub

End Class





ASP.NET Developer's Cookbook, By Steven Smith, Rob Howard, ASPAlliance.com 

ASP.NET By Example, By Steven Smith 




Steven Smith, MCSE + Internet (4.0)
Last Modified: 6/12/2009 10:58:20 AM
History: 6/12/2009 10:58:20 AM