How to use httpModules to troubleshoot your ASP.NET application

No matter how many things you keep in mind before developing an application, when once they are put in production... quite often you will see something not behaving the way you expected. Many-a-times, the applications would throw an error which you can troubleshoot depending on the error message you found. In this post, my intention is not to cover those "error" scenarios. I intend to cover something which is more problematic to troubleshoot... like intermittent slowness in some pages, or some errors that are coming up and you are not able to locate what's wrong with those bad-pages.

What would you do in those situations??

I know there is not a single correct answer to this question and it is scenario specific. Let me show you one little trick which might help you while troubleshooting your web-applications. This application (module) is no-where near completion... it just shows you how you can use Modules to troubleshoot the issue. You can tweak the code based on your scenario and work accordingly. I will share the plain httpmodule in this post. Once I am done with this module with lots of other automated configuration stuff (like capturing all viewstate, session information, etc), I will update my blog accordingly.

Let's proceed by creating a class Library (I will call it myModules)

1. Add a reference to System.Web
2. Copy the following in a new class file called Trigger.cs

 using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace myModules
{
    class Trigger:IHttpModule
    {
        DateTime fTimerValue;
        public void Init(System.Web.HttpApplication context)
        {
            context.BeginRequest += new EventHandler(this.BeginRoutine);
            context.EndRequest += new EventHandler(this.EndRoutine);
        }
        public void BeginRoutine(object o, EventArgs e)
        {
            fTimerValue = DateTime.Now;
        }
        public void EndRoutine(object o, EventArgs e)
        {
            HttpApplication myApp;
            TimeSpan t = TimeSpan.MinValue;
            myApp = (HttpApplication)o;
            HttpContext c = myApp.Context;
            if(c.Response.StatusCode == 200)
            {
                t = DateTime.Now - fTimerValue;
                if (t.TotalSeconds > 5)
                {
                    c.Response.Write("Tweak this page!! It took... " + t.TotalSeconds + " seconds");
                    c.Response.Write("<BR>");
                    c.Response.Write("Name > " + c.Request.PhysicalPath);
                    return;
                }
            }
            myApp.Context.Response.Write("This response took " + t.ToString() + " seconds");
        }
        public void Dispose()
        {
            //Nothing to Dispose as of now
        }
    }
}

3. In your WebApplication... drop the myModules.dll from the class library that you just created.

4. Last step... modify the web.config by adding...

         <httpModules>
            <add name="Trigger" type="myModules.Trigger, myModules"/>
        </httpModules>

5. I tried this with an Empty project. And introduced Sleep statement in the Page_Load...

     protected void Page_Load(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(26000);
    }

6. Notice that, I haven't recompiled my web-application. Only thing that is required is... copy the DLLs in the bin folder and add httpModules section in the web.config.

The output is...

image 
As I said earlier... this is a very meager output, and not enough to fix the issue. But now that you know how to use Modules to troubleshoot your application, feel free to tweak around the code and fix the issue at hand.

Happy troubleshooting! Smile

Rahul

Quote of the day:
An idealist is one who, on noticing that a rose smells better than a cabbage, concludes that it will also make better soup. - H. L. Mencken

Comments