ASP.NET:How to write error messages into a text file using a simple httpModule

by Rahul 7/18/2008 12:56:11 AM

In one of my previous posts, I mentioned about how to troubleshoot some issues with the use of a module. In this post, I will show you how a similar module could be of use when you want to log all the errors in a text file for troubleshooting purposes. Please ensure that C:\Temp folder has Write access for the account that is running the IIS Worker Process. Here is the code for the module...

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
namespace myModules
{
    class Trigger:IHttpModule
    {
        public void Init(System.Web.HttpApplication context)
        {
            context.EndRequest += new EventHandler(this.EndRoutine);
        }
        public void EndRoutine(object o, EventArgs e)
        {
            HttpApplication myApp;
            myApp = (HttpApplication)o;
            HttpContext c = myApp.Context;
            if (myApp.Context.AllErrors != null)
            {
                StreamWriter sw = new StreamWriter("C:\\Temp\\ErrorList.txt", true);
                foreach (Exception ex in myApp.Context.AllErrors)
                {
                    sw.WriteLine("Error occurred at #{0}", DateTime.Now.ToString());
                    sw.WriteLine("Error occurred in page #{0}", c.Request.FilePath);
                    sw.WriteLine(ex.InnerException.Message);
                    sw.WriteLine(ex.InnerException.StackTrace);
                    sw.WriteLine();
                }
                sw.Dispose();
            }
        }
        public void Dispose()
        {
            //Nothing to Dispose as of now
        }
    }
}
Download

You can save the downloaded file to the bin folder of your web application and add the following to the web.config...

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


Here is the sample output (C:\Temp\ErrorList.txt) for one of the applications where I have used the Throw New AppDomainUnloadedException or similar lines to create the error...

image

Hope this helps, Happy
Rahul


Quote of the day:
The nice thing about standards is that there are so many of them to choose from. - Andrew S. Tanenbaum



Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

ASP.NET | HttpModules | Tips and Tricks | Troubleshooting

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Calendar

<<  January 2009  >>
MoTuWeThFrSaSu
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway. We try our best to write good code, but none of the code here is tested on Production boxes. Feel free to use the code, BUT test it before you do so (in simple words... use it at your own risk)

© Copyright 2009

Sign in