Sunday, October 11, 2009

Global error handling in ASP.NET and Windows Forms

You should not let exceptions unhandled in *any* part of your application. But that doesn't mean you have to spray your entire code with a swarm of try/catch blocks. Both Windows Forms and ASP.NET applications offer the possibility to create a global error handler, that will be activated whenever an unhandled exception (i.e., an exception that has not happened inside a try/catch block) occurs.

Windows Forms Global Exception Handler

As always, things are easier in Windows Forms than in ASP.NET. Windows Forms provides the developer with the Application object, that among other things has the ThreadException event. Basically, all you have to do is to add an even handler to the ThreadException event, and your global event handler is set.

Create a new VB.NET Windows Forms project, add a button to the Form1 form, and replace its code with the following:


Imports System.Diagnostics

Public Class Form1

Const MYAPP_SOURCE_NAME As String = "My Application"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Creates the event source on Windows Application Log
' This has to be done only once, usually during app setup
If Not EventLog.SourceExists(MYAPP_SOURCE_NAME) Then
EventLog.CreateEventSource(MYAPP_SOURCE_NAME, "Application")
End If
' Designates the global error handler
AddHandler Application.ThreadException, AddressOf Me.TheGlobalErrorHandler
End Sub

Sub TheGlobalErrorHandler(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
' This is how we get the exception data
Dim unhandledException As Exception = e.Exception
' Writes technical error information to the Application Log
EventLog.WriteEntry(MYAPP_SOURCE_NAME, _
"Unhandled error: " + vbNewLine + unhandledException.ToString(), _
EventLogEntryType.Error)
' Some nice message to the user
MsgBox("An unexpected error has occured. Technical error information " & _
"has been writen to the Windows Application Log.", _
MsgBoxStyle.Critical)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Simulates an unhandled exception
Throw New Exception("Oh oh error accessing the database")
End Sub
End Class



Click on the button to simulate an unhadled exception. A message is displayed and if you look at the Windows Application Log on EventViewer (Start > Run > eventvwr.msc), there it is the exception.

ASP.NET Global Exception Handler

ASP.NET provides you with the Application_Error global application event, that is fired whenever an unhandled exception occurs. This event handler belongs to the global application class, as Visual Studio calls it. The global application class code is located on the Global.asax file. ASP.NET applications, by default, don't have a Global.asax file. You can add one by going to the Project menu and selecting Add New Item > Global Application Class.

On the Application_Error event handler code, you have to use two special methods:
  • Server.GetLastError() gives access to the unhandled exception.
  • Server.ClearError() prevents ASP.NET from showing the default error page.
Create a new ASP.NET Web Application, add a Global.asax file to it, and replace the code in Global.asax.vb with the following:

Imports System.Web.SessionState
Imports System.Diagnostics

Public Class Global_asax
Inherits System.Web.HttpApplication

Const MYAPP_SOURCE_NAME As String = "My Application"

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Creates the event source on Windows Application Log
' This has to be done only once, usually during app setup
If Not EventLog.SourceExists(MYAPP_SOURCE_NAME) Then
EventLog.CreateEventSource(MYAPP_SOURCE_NAME, "Application")
End If
End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Stores the unhandled exception
Dim unhandledException As Exception = Server.GetLastError()
' Says to the Framework "Don't show default error page - we will handle it"
Server.ClearError()
' Writes techinical error information to the Application Log
EventLog.WriteEntry(MYAPP_SOURCE_NAME, _
"Unhandled error: " + vbNewLine + unhandledException.ToString(), _
EventLogEntryType.Error)
' Redirects user to some page with nice error message
Response.Redirect("/error.aspx")
End Sub

End Class


Create a page named "error.aspx"; that's the page users will be redirected to after an unhandled exception.

Finnaly, add a Button to Default.aspx, and replace its code with the following:

Partial Public Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
' Simulates an unhandled exception
Throw New Exception("Oh oh that database access error again")
End Sub
End Class


Click on the button and you will be redirected to error.aspx, and an event will be logged on the Application Log with the exception data.



UPDATE: the Application_Start code will fail on Windows Vista & Windows 7, because of tightened security. The code tries to create an event source in order to log events to it. The methods EventLog.SourceExists() and EventLog.Create() both try to scan the Windows Logs to check if the specified source exists, but access to the Security Log by ASP.NET apps is not allowed by default. There are several ways to "do it right", but a quick and dirty solution is to create the following registry key: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\eventlog\Application\YOUR_APP_NAME, changing YOUR_APP_NAME to the event log source you want to create. A possible way to "do it right" is to create a command line app containing the Application_Start code, and set it to run from your site's setup package.

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru Dot Net Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete