Use HttpModule to turn tracing on via the querystring

Sometimes I want to turn tracing on in a production application to troubleshoot issues that I can’t replicate on my development machine.  It’s easy enough to do this by turning on application-level tracing in the web.config file, like so:

<configuration>
 <system.web>
  <trace enabled=”true” pageOutput=”true” />
 </system.web>
</configuration>

The problem with this is that changes to the web.config file cause the app domain to recycle, which can cause users currently in the application to lose data.  So I have to schedule a time when all users have to get out, or I have to work after hours, which is a drag.

It’s easy enough to turn on tracing programmatically – Page.Trace.IsEnabled = true.  Then you could write code in the page’s Load event, turning tracing on if the page detects a particular querystring.  The problem here is that you’d have to add the code to every page that you might someday want to trace.

So I decided to create an HttpModule that would turn on tracing via the querystring.  HttpModules allow you to tap in to the HttpRequest pipeline, so your code will run at a designated point in every ASP.NET request.  You can hook up your module to run at various points in the page life cycle, such as BeginRequest, AuthenticateRequest, Error, etc.  (HttpModules are also excellent for application-level error handling). 

In order to create an HttpModule, you have to create a class that implements the IHttpModule interface.  Do this by creating your class, and then typing “Implements IHttpModule” directly under the “Public Class Whatever” line.  Once you hit enter, the two methods that your class must invoke, Init and Dispose, are automatically created for you.

Here’s what my HttpModule class looks like (be sure to import the System.Web namespace):

Option Strict On

Imports Microsoft.VisualBasic
Imports System.Web

Public Class TraceModule
    Implements IHttpModule

    Dim WithEvents app As HttpApplication = Nothing

    Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init
        app = context
    End Sub

    Public Sub context_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) _
    Handles app.BeginRequest

        Dim c As HttpContext = HttpContext.Current
        Dim strTrace As String = c.Request.QueryString(“trace”)

        If strTrace = “on” Then
            c.Trace.IsEnabled = True
        End If

    End Sub

    Public Sub Dispose() Implements IHttpModule.Dispose
        app = Nothing
    End Sub

End Class

After you’ve created your module, you just need to add a setting in the web.config file telling ASP.NET to use your module, as shown below.  If the class is in an assembly in the bin folder, you just need to provide the class name.  If it’s in the App_Code folder, then you need to add that to the setting.

<configuration>
 <system.web>
  <httpModules>
   <!– Module class in assembly called TKS.dll in bin folder –>
   <add name=”WebTraceModule” type=”TKS.TraceModule”/>
   <!– Module class in App_Code folder –>
   <add name=”WebTraceModule” type=”TraceModule, App_Code” />
  </httpModules>
 </system.web>
</configuration>

Now if you want to see the trace for any page in your application, just add the querystring “?trace=on” at the end of the URL, and the trace will display in the page without making any other changes.  If you want to add some security to this, so that not just anybody can run the trace, you can require additional querystrings, or only turn tracing on for certain users or groups, or just about anything else that you can do in code.

Advertisements