When developing my applications I was looking for a global way of handling my exceptions.
There are some packages you can you for handling your exceptions.
First I used the Enterprise Library Exception Handling Block but today I changed from the Enterprise Library to another library called elmah.
In the next few minutes I will explain how to use the elmah package to handle and log your exception to a database.
First of all for our example I created a new Web Application. (In my case it is called ElmahExceptionHandling).
After creating the project I created an error page that will be used to redirect to that gives a nice and userfriendly message to the user. There I return the current date (+time) and the message that the users can call the helpdesk to examine their problem by giving this date. With that date the helpdesk can take a look in the database or at the elmah.axd file to check what the error message was.
After creating the Error page we will redirect to it in the Application_Error method in the global.asax. That is the method that is called when the application gets an exception.
By making use of the global.asax error method we don’t use specific exception handling in our application (so we never use a try catch block, unless we want some specific handling in a specific method).
To make our application redirect to the Error page when needed we add following code to the global.asax:
void Application_Error(object sender, EventArgs e)
// Code that runs when an unhandled error occurs
Now we need to make use of the NuGet package called Elmah to catch and handle our exceptions.
We will need to add ‘elmah’, ‘elmah.corelibrary’ and ‘elmah.sqlserver’. This last one is needed to create the sql tables to log all your exceptions to the database.
You will see that there is a App_Readme folder created into your project. There is a .sql file that will create the elmah table that is needed.
So next we will create the database.
You can take an existing database or create a new one and execute the script. I create a database ElmahExceptions and created there (through the sql script) the ELMAH_Error table. My database looks like this:
Now that our database is created we can set our connectionstring and create our web.config changes.
You will see that in the web.config file there is a line added:
<add name="elmah-sqlserver" connectionString="Data Source=****;User ID=****;Password=****;Initial Catalog=****;" providerName="System.Data.SqlClient" />
You need to change this to your database connectionstring. After changing my connectionstring looks like this:
<add name="elmah-sqlserver" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=ElmahExceptions;" providerName="System.Data.SqlClient" />
To test this exception handling I put following code in the default.aspx page:
protected void Page_Load(object sender, EventArgs e)
throw new Exception("TestException");
This wil create a new exception and the site will redirect to Error.aspx with the userfriendly message. My error page looks like this:
Now we can go to our “elmah.axd” page. Here we get an overview of all the errors. Now there will be an error for our own exception. The page will look like this:
And of course the error will also be logged in our database. If we take a look at the database we can see a new record that looks like this:
And so far my way of handling and logging my exceptions.