środa, 5 sierpnia 2009

Add ELMAH to MVC application

ELMAH (Error Logging Modules And Handlers) is the way logging runtime errors in a production environment. ELAMAH is open source library.
In few steps I show how add this library to mvc application.

1. Add elmah.dll assembly to your project. Assembly file you can find in ELMAH website
2.Registering ELMAH's HTTP Module and Hander
You must register ELMAH in web application configuration.
Start by adding to your section in web.config ErrorLogModule:


<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />


Next add to section Elmah module:


<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>


Next add to section following module and handler:


<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
...

<add name="Elmah.ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
</modules>
<handlers>
...

<add name="Elmah" path="elmah.axd" verb="POST,GET,HEAD" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
</handlers>
</system.webServer>


3. Configure ELMAH
In web.config you must defined :


<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
...

<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
</sectionGroup>
</configSections>

Next add section:


<?xml version="1.0"?>
<configuration>
...

<elmah>
<security allowRemoteAccess="0" />

<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ReviewsConnectionString" />
</elmah>

...
</configuration>


...
</configuration>