SIGN UP MEMBER LOGIN:    
ARTICLE

Logging Errors with ELMAH

Posted by Subhendu De Articles | Error Zone January 10, 2011
ELMAH (Error Logging Modules and Handlers) is a pluggable component that you can use to log errors without modifying the application code.
Reader Level:


ELMAH (Error Logging Modules and Handlers) is a pluggable component that you can use to log errors without modifying the application code. The main advantage of ELMAH is it's pluggable feature. You can easily integrate the ELMAH component in your developed application. It's an open source project and you can customize the code according to your needs. I am not going to show you how to customize the component; instead configuration of this component with your ASP.Net application and how you can store the error messages in SQL Server. You can also store the error information in Oracle, Access or XML. There are several options available in their website.

First task is to download the ELMAH project from following link

http://code.google.com/p/elmah/

Once download is complete, create one website and add the elmah.dll reference from bin folder under the downloaded folder. All versions dll are available starting from .Net fx 1.1 to .Net fx 3.5.

1.gif

Next, make the appropriate ELMAH entries in Web.config. In ConfigSections, add the following:

<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>

2.gif

Add this top level element:

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

3.gif

Under httpModules add this line:

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

3.5.gif

Under httpHandlers, add:

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

4.gif

Finally, add this top level element:

<location path="elmah.axd">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>

5.gif

That's all. You completed the configuration to start the ELMAH.

You can now see the elmah.axd file. Open your browser and type http://localhost:%3Cport%3E/elmah.axd

6.gif

You will now create some exception intentionally like divide by zero, calling the page which does not exist etc.

Create a webform and put the following code in page_load section.

int temp = 0;
Response.Write(Convert.ToString(125 / temp));


Run the page and you will see the "yellow death screen" page.

Again run the elmah.axd page and you will find the exception details logged in the page. Try to access the page which doesn't exist in application and the exception logged in elmah.axd page. So any application exception will log and display in elmah.axd file.

Untill this point, I did not change any application specific code. I wrote some configuration in web.config and used the elmah.dll and my exception logging starts. That is the main beauty of ELMAH.

Moving next, I will show you how to log this exception details in SQL Server database. The first task is to create the tables in the database. Open the downloaded ELMAH folder and locate the SQLServer scripts under db folder and run against your database. The ELMAH_Error table and stored procedures will be created.

7.gif

Add a connection string element in web.config to point to the database.

<connectionStrings>
    <
add name="ElmahConnectionString" connectionString="Data Source=.\sqlexpress;Initial Catalog=EFBlogs;Integrated Security=True;Pooling=False"/>
  </connectionStrings>

Add following code in web.config under top level <elmah> tag.

<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ElmahConnectionString"/>
That's it. Run the pages that will raise exception and you will see the exception details starts logging in ELMAH_Error table.
  

Login to add your contents and source code to this article
share this article :
post comment
 

hi this information is really useful. From what i understand the ELMAH only records runtime errors what if you want to record compile time errors. Can u give some hints like class or API that could help me. My task is to build a .DLL that could be referenced in any application(for now only c#) to record compile errors in debug mode to a database. It would be really helpful

Posted by ahsan sadeque Sep 22, 2011

You can do it. Download the ELMAH sourcecode and add new class EventErrorLog... namespace Elmah { public class EventErrorLog : ErrorLog { public override string Log(Error error) { // code to write in EventLog } public override ErrorLogEntry GetError(string id) { // code to write in list of errors } public override int GetErrors(int pageIndex, int pageSize, System.Collections.IList errorEntryList) { throw new NotImplementedException(); } } }

Posted by Subhendu De Jan 10, 2011

Is it possible to log exceptions to the Application Events log? One of them that the Event Viewer shows? Perhaps there is a good reason to not do that, such as too much data, but it seems to be a useful option. I looked in the ELMAH page and did not see a mention of use of the Application Events log. Regardless, ELMAH does look useful; thank you for tellling us about it.

Posted by Sam Hobbs Jan 10, 2011

Yes, it was.... Thanks....

Posted by Subhendu De Jan 10, 2011

Really its a cool plugin, In one of my project, I have integrated this plugin. Very simple to integrate in any projects, It can be plugged into any of the existing projects also. Very Few Steps to Plug In. User Interface of displaying the error reports is also good.

Posted by Manikavelu Velayutham Jan 10, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor