by
Venkata Koppaka
| Dec 05, 2009
Off late I have been using many of open-source projects in the websites that I am trying to build.
Elmah is one among them - Click Here to know more about Elmah.
Here is my quick take on how to use Elmah in your website.
NOTE: This demo application is built using .NET Framework 4.0 and Visual Studio 2010 Beta2.
To start using Elmah in your website, include Elmah.dll in to your website. Go to the website right-click on the solution and say add reference
Point to the Elmah.dll that you have downloaded from google code and it as it as a reference in your project.
Next in your web.config add the following changes. -
- Adding Elmah Configuration Section
| 1 | <configSections> |
| 2 | <sectionGroup name="elmah"> |
| 3 | <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah"/> |
| 4 | <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah"/> |
| 5 | <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/> |
| 6 | </sectionGroup> |
| 7 | |
| 8 | </configSections> |
2. Add Elmah Connection String
| 1 | <connectionStrings> |
| 2 | <add name="ElmahConnectionString" connectionString="Data Source=SUBBARAO-PC\SQLEXPRESS;Initial Catalog=ElmahDatabase;Persist Security Info=True;User ID=sa;Password=sql" providerName="System.Data.SqlClient"/> |
| 3 | </connectionStrings> |
3. Add Elmah Section to your Web.config
| 1 | <elmah> |
| 2 | <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ElmahConnectionString"/> |
| 3 | <!-- Don't log 404 --> |
| 4 | <errorFilter> |
| 5 | <test> |
| 6 | <equal binding="HttpStatusCode" value="404" valueType="Int32"/> |
| 7 | </test> |
| 8 | </errorFilter> |
| 9 | </elmah> |
4. Add Custom Error Page ( Please note that this step is optional) you may redirect the client to a generic error page or let the Yellow Screen of Death be shown. If you choose to include a generic error page, include the following lines in the web.config
| 1 | <customErrors mode="On" defaultRedirect="~/Error.aspx"/> |
5. Add HttpHandlers and HttpModules that Elmah needs. -
| 1 | <httpHandlers> |
| 2 | <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah"/> |
| 3 | </httpHandlers> |
| 4 | <httpModules> |
| 5 | <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/> |
| 6 | </httpModules> |
That is all the configuration we need to do to get Elmah up.
As for an example that accompanies Elmah I wrote the sample that can be downloaded. This is what I wrote to simulate Elmah.
I added a Button Click method which throws a NotImplemented Exception . When the website is redirected to a generice Error page (Error.aspx in this case). It can tell the client whatever error message we want to convey them.
Then hit www.yourwebsite.com/elmah.axd to view a error log which would look similar to this

The view details of Elmah would show you exactly the Yellow Screen of Death that your client sees.
Everything above should get you up and running for using Elmah.
Phil Haack has an excellent article to secure Elmah here . Scott Hanselman has an article to show you how to use Elmah with ASP.NET MVC here.
Hope this article helps .
You can download all the Code for this article here
The Script that is need to be run against a SQL Database could be found here
Cheers,
Venkata