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

by Rahul 7/18/2008 7:26:11 PM

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



blog comments powered by Disqus

Comments

Rahul Soni

Rahul Soni  Twitter

 LinkedIn

 Facebook

 Email me



Vivek Kumbhar

Vivek Kumbhar  Twitter

 LinkedIn

 Facebook

 Email me


Stack Exchange

profile for Vivek at Server Fault, Q&A for system administrators and IT professionals

profile for Rahul Soni at Stack Overflow, Q&A for professional and enthusiast programmers

Calendar

<<  May 2013  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

All Items
Sign in

Visit Microsoft's Site

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2013, Rahul Soni

Powered by BlogEngine.NET 1.4.5.0