User Friendly ASP.NET Exception Handling

August 21, 2004

I just posted a new article to CodeProject, User Friendly ASP.NET Exception Handling.

I casually mentioned in the original article that I didn't think a global unhandled exception management class designed for WinForms and console apps was appropriate for ASP.NET apps, and that I had a seperate-but-equal class I used in those scenarios. Well, based on that vague comment, I kept getting requests for this code. So after a recent email I was motivated to finish up the sample solution. It's not as exciting as the client version (naturally, it doesn't capture screenshots of the desktop) but it's still extremely useful in all the production systems that I work on.

I think in the future I'll avoid using my blog to host code of any significance. It gets better exposure on CodeProject, and formatting the article for that general "everyprogrammer" audience motivates me to write much more comprehensive documentation than I otherwise would. I've also gotten some truly excellent feedback/suggestions from developers in the comments. Overall, a really positive experience, a great community, and I wholeheartedly recommend it.

Posted by Jeff Atwood
1 Comments

I added code for custom errors support in web.config.
Please check it. If you like,please add to your code project article.

Protected Overridable Sub OnError(ByVal sender As Object, ByVal args As EventArgs)
Dim app As HttpApplication = CType(sender, HttpApplication)
If InStr(app.Server.GetLastError.Message, "The file") 0 And InStr(app.Server.GetLastError.Message, "does not exist") 0 Then
Dim ueh As New Handler
ueh.SaveExceptionToLogFile(app.Server.GetLastError)
app.Server.ClearError()

Dim virtualPath As String = Sahin.Diagnostics.Errorlog.Config.GetString("AppPath")
Dim config As Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(virtualPath)

Dim cusErr As Web.Configuration.CustomErrorsSection
cusErr = CType(config.SectionGroups("system.web").Sections("customErrors"), Web.Configuration.CustomErrorsSection)
If cusErr.Mode = Web.Configuration.CustomErrorsMode.On Then
For Each err As System.Web.Configuration.CustomError In cusErr.Errors
If err.StatusCode = 404 Then
app.Response.Redirect(err.Redirect)
End If
Next
End If
Else
Dim ueh As New Handler
ueh.HandleException(app.Server.GetLastError)
End If
End Sub

Mesut Sahin Boydas on February 18, 2006 9:38 AM

The comments to this entry are closed.