Using Elmah for Error Handling

8
Elmah Using for Error Handling 7/1/2009

Transcript of Using Elmah for Error Handling

Page 1: Using Elmah for Error Handling

Elmah

Using for Error Handling

7/1/2009

Page 2: Using Elmah for Error Handling

IntroductionA solution is required to gracefully handle application errors on all CMS applications. The process should capture details of the error, while presenting a friendly error page to the end user. This document describes the implementation of the ELMAH (Error Logging Modules and Handlers) solution found at http://code.google.com/p/elmah/

This document assumes that the ELMAH database has been created and is online.

Page 3: Using Elmah for Error Handling

Setup InstructionsBelow are setup instructions for a specific application (web.config) only. Alternatively, ELMAH can be applied machine wide (machine.config) to make error handling available to all applications simultaneously.

SQL Server Database SetupA new database must be created in order to save error logs. The database can have any name (defined in the web.config errorLog connection string). A script is available to create the table for storing the errors.

Error Logging SetupThe steps below outline the setup process in order to activate error logging for a specific application.

1. Copy GotDotNet.Elmah.dll to the application Bin folder2. Open web.config and add the entries displayed below

Web.config Entries

Add to <configSections>

<sectionGroup name="gotdotnet.elmah"> <!-- Indicates that inside the section group there will be an errorLog section --> <section name="errorLog" type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <!-- Indicates that inside the section group there will be an errorMail section --> <section name="errorMail" type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </sectionGroup>

Page 4: Using Elmah for Error Handling

Add after </configSections>

<!-- The section group is specific to configuration of ELMAH --> <gotdotnet.elmah> <!-- This section contains the type of the error logger to use (SqlErrorLog, MemoryErrorLog, or a custom logger). as well as the properties pertinent to the error logger (e.g., connectionString for the SqlErrorLog). --> <errorLog type="GotDotNet.Elmah.SqlErrorLog, GotDotNet.Elmah, Version=1.0.5527.0, Culture=neutral, PublicKeyToken=978d5e1bd64b33e5" connectionString="Server=(local);Database=ELMAH;Trusted_Connection=True;" /> <!-- To have an administrator e-mailed with details of the exception, uncomment the following section. You MUST specify a to e-mail address; you can optionally specify a from (default is same as to); (consult the code for ErrorMailModule for more optional setting values) --> <errorMail from="[email protected]" to="[email protected]" subject="ELMAH Error Report" async="false" smtpServer="255.255.255.255" /> </gotdotnet.elmah>

Add to <httpHandlers>

<!-- Register that a request to elmah/default.aspx should be serviced by the ErrorLogPageFactory HTTP Handler factory --> <add verb="POST,GET,HEAD" path="elmah/default.aspx" type="GotDotNet.Elmah.ErrorLogPageFactory, GotDotNet.Elmah, Version=1.0.5527.0, Culture=neutral, PublicKeyToken=978d5e1bd64b33e5" />

Add to <httpModules>

<!-- Adds the ErrorLogModule to the HTTP pipeline. --> <add name="ErrorLog" type="GotDotNet.Elmah.ErrorLogModule, GotDotNet.Elmah, Version=1.0.5527.0, Culture=neutral, PublicKeyToken=978d5e1bd64b33e5" /> <!-- Adds the ErrorMailModule to the HTTP pipeline. --> <add name="ErrorMail" type="GotDotNet.Elmah.ErrorMailModule, GotDotNet.Elmah, Version=1.0.5527.0, Culture=neutral, PublicKeyToken=978d5e1bd64b33e5" />

Page 5: Using Elmah for Error Handling

Add after </system.web>

<!-- Deny unauthenticated users to see the ~/elmah --> <location path="elmah"> <system.web> <authorization> <deny users="?"/> </authorization> </system.web> </location>

Make sure that <customErrors> mode is set to “On” with a defaultRedirect to display a friendly error page.

<customErrors mode="On" defaultRedirect="~/Application_Error.aspx"/>

Page 6: Using Elmah for Error Handling

Viewing the Error LogThe error log can be viewed by accessing the following path:

http(s)://<application_path>/elmah/default.aspx

Error log page

Page 7: Using Elmah for Error Handling

Error Details Page

Page 8: Using Elmah for Error Handling

ASP.NET Error Message in Full View