Introduction
I wrote this HTTP module for a client to plug in an existing site which was having intermittent errors while undergoing integration testing. The client wanted something that could capture the basic error information along with the request query-string/form parameters and write it out. All this needed to be done without modifying the existing code.
Although the existing code did have regular try
-catch
error handlers in the code-behind files, not all errors were handled properly, and in some cases, the code in the catch
block itself was error-prone. Initially, I thought of using a custom error page. However, in view of issues regarding getting the correct context of errors, I decided to use an HTTP module.
The code
The code itself is pretty straightforward.
Step 1 is to wire an event handler for the Error
event:
public void Init (HttpApplication app)
{
app.Error += new System.EventHandler (OnError);
}
Step 2 involves writing the actual event handler to write out the error message and the request form / query-string parameters:
public void OnError (object obj, EventArgs args)
{
HttpContext ctx = HttpContext.Current;
HttpResponse response = ctx.Response;
HttpRequest request = ctx.Request;
Exception exception = ctx.Server.GetLastError();
response.Write("Your request could not processed. " +
"Please press the back button on" +
" your browser and try again.<br/>");
response.Write("If the problem persists, please " +
"contact technical support<p/>");
response.Write("Information below is for " +
"technical support:<p/>");
string errorInfo = "<p/>URL: " + ctx.Request.Url.ToString ();
errorInfo += "<p/>Stacktrace:---<br/>" +
exception.InnerException.StackTrace.ToString();
errorInfo += "<p/>Error Message:<br/>" +
exception.InnerException.Message;
response.Write("Querystring:<p/>");
for(int i=0;i<request.QueryString.Count;i++)
{
response.Write("<br/>" +
request.QueryString.Keys[i].ToString() + " :--" +
request.QueryString[i].ToString() + "--<br/>");
}
response.Write("<p>---------------" +
"----------<p/>Form:<p/>");
for(int i=0;i<request.Form.Count;i++)
{
response.Write("<br/>" +
request.Form.Keys[i].ToString() +
" :--" + request.Form[i].ToString() +
"--<br/>");
}
response.Write("<p>-----------------" +
"--------<p/>ErrorInfo:<p/>");
response.Write (errorInfo);
ctx.Server.ClearError ();
}
Deployment:
To deploy this HTTP module, simply drop the compiled DLL in the bin folder and make this entry in the web.config to register it:
<system.web>
-->
<httpModules>
<add type="MyApps.Utilities.GlobalErrorHandler,
GlobalErrorHandler"
name="GlobalErrorHandler" />
</httpModules>
</system.web>
Download
You can download the complete source code (.cs file), a sample web.config, and the compiled DLL in a single zip file GlobalErrorHandler.zip from the link above.
Further Improvements
This code was really a result of a couple of hours work, so it can obviously be refined. A few changes I am already considering: make the display text dynamic and the overall display more customizable via web.config. In addition, an option to email or log the error messages would be useful. I will be posting updated code in the next few weeks.