Posted by beakersoft | Posted in Programming | Posted on 23-05-2008
I’ve been in the situation a few times with ASP.Net applications where someone says to you ‘I got this error on screen when I did x, I don’t know what it said but i couldn’t work.’
Odds are you have got an unhanded exception in your web application, but how are you meant to debug it when the error has long gone? Elmah (error logging and error handling modules for asp.net) may well be your saviour. Download it out now from http://code.google.com/p/elmah/
So, what does it do?
Well, once you have it configured on your application or server, every time something causes an unhanded exception it will log it into a database, send you an email alert and fix the code for you (last part was a joke!). So, now when the user tells you they have had an problem you can look in the system and see exactly what happened.
The application is open source, and is based on the article at http://msdn.microsoft.com/en-us/library/aa479332.aspx, check that article out if you want to know more about how it was written
You can run it in two ways, on a particular web application or at the server level. As I was a bit confused at first as to how to set it up, here is a quick guide to installing it for an individual application and log the errors onto a SQL 2000 server.
- Download the zip file from the website, if you are using dotnet 2.0 you will need the latest beta version.
- Extract the files, in the src/Elmah/ folder find the Database.sql script. This is the script that will build the tables it needs. Create a new database and run this script on the database
- Next, go into the bin/net-2.0/Release/ folder, and copy the Elmah.dll and Elmah.xml files into you applications /bin folder
- Once you have the assembly, you need to configure your application to use it. You do this by adding some sections into your applications web.config file. In the /samples folder there is an example web.config file you can get all the information out of to put in your file. For the most part it is easy to follow, but I had a couple of problems.
- The first one was the database connection, make sure you add a new connection to your <connectionStrings> section that points at the database you have created.
- You then need to point to this connection in the connectionStringName section of the elmah errorLog.
- Once you have added the other sections into your config file you should just be able to browse to your site /elmah.axd to see your errors
You can also configure options such as getting it to send an email when an exception occurs, subscribe to the log as an rss feed etc.
So now you should have a handy reference point for all your exemptions. You can tell the user you are looking into there problem before they even report it to you!