views:

15

answers:

0

I have set up a HTTPModule that intercepts an ASP.NET applications OnError event, to log the error in a database. However, what I am finding is that if the exception occurs in a loop, the page seems to continue executing (with all the errors logged as well).

This is the code for the HTTPModule's OnError event:

ETILibConfiguration err = ETILibConfiguration.GetConfig();

HttpContext ctx = HttpContext.Current;
HttpResponse response = ctx.Response;
HttpRequest request = ctx.Request;

// Fires when an error occurs
StringBuilder innerExceptionText = new StringBuilder();
int i;

string errorMessage = string.Empty;
string errorStackTrace = string.Empty;
string errorBaseMessage = string.Empty;
string errorBaseStackTrace = string.Empty;
string errorInnerText = string.Empty;

Exception e = ctx.Server.GetLastError();

// Retrieve the top-level exception's information. In unhandled errors, this is typically
// an instance of the HttpUnhandledException class, which contains bugger all information.
errorMessage = e.Message;
errorStackTrace = e.StackTrace;

// Retrieve the first exception that occurred. If only one exception has occurred, then this
// method returns the exact same information has the top-level exception.
errorBaseMessage = e.GetBaseException().Message;
errorBaseStackTrace = e.GetBaseException().StackTrace;

// Dump all the inner exceptions, and session/querystring variable information
errorInnerText = innerExceptionText.ToString();

// Clear the last error.
ctx.Server.ClearError();

// Add the error to the error log.
int? errorNumber;

// Create a new instance of the error handling class, passing in the database connection string.
ErrorManager errorinst = new ErrorManager();

ErrorEntry error = new ErrorEntry();
error.UserName = ctx.User.Identity.Name;
error.Application = "TestApplication";
error.BaseExceptionText = errorBaseMessage + Environment.NewLine + errorBaseStackTrace;
error.InnerExceptionText = errorInnerText;
error.ExceptionText = errorMessage + Environment.NewLine + errorStackTrace;

error = errorinst.SaveError(ref error);

errorNumber = error.ErrorID;


// Automatic redirection.
response.AppendHeader("Refresh", "5; url=" + errorHomepage);

// Retrieve the HTML from the resource: ErrorHTML
string html = Properties.Resources.ErrorHTML;

// Write the HTML out to the response stream.
response.Write(html);

ctx.ApplicationInstance.CompleteRequest();

ApplicationInstance.CompleteRequest doesn't seem to fix the problem, and calling response.end throws another exception. What I have found is that if I response.redirect to another page in the application, whilst using False for the second parameter, it does abort correctly.

Any ideas?