Quantcast
Channel: eXpandFramework RSS
Viewing all articles
Browse latest Browse all 861

BLOG by Robert Anderson: ELMAH with DevExpress XAF

$
0
0

ELMAH (Error Logging Modules and Handlers) is an open source library for logging unhandled exceptions. This post explains how to get it running with the DevExpress XAF main demo.

A couple of amazing facts about ELMAH.

  • It has been around since 2004!
  • It was written by Atif Aziz who happens to be an old school-friend from the International School of Geneva.

XAF provides quite extensive error handling options out of the box, but I have found Elmah better suited to production environments because of the ability to remotely view the full error log.

Setting up

First, get the ELMAH package via NuGet into the MainDemo.Web project. ELMAH provides dozens of different methods of persisting the error log. For this example we’ll choose one of the simplest. Make sure you select the ELMAH on XML Log package.

NuGet makes several automatic modifications to the web.config. Unfortunately, these are not quite accurate enough for XAF. The changes you need to make are detailed below:

Add a <configSection> for ELMAH as alongside the existing devExpress one.

web.config
123456789
<configSections><sectionGroupname="devExpress">...</sectionGroup><!-- this should already exist--><sectionGroupname="elmah"><!-- this is new--><sectionname="security"requirePermission="false"type="Elmah.SecuritySectionHandler, Elmah"/><sectionname="errorLog"requirePermission="false"type="Elmah.ErrorLogSectionHandler, Elmah"/><sectionname="errorMail"requirePermission="false"type="Elmah.ErrorMailSectionHandler, Elmah"/><sectionname="errorFilter"requirePermission="false"type="Elmah.ErrorFilterSectionHandler, Elmah"/></sectionGroup></configSections>

Your <system.webServer> section should look like this:

web.config
12345678910
<system.webServer><handlers>...</handlers><!-- This is unchanged --><validationvalidateIntegratedModeConfiguration="false"/><modules><addname="ASPxHttpHandlerModule"type="DevExpress.Web.ASPxClasses.ASPxHttpHandlerModule, DevExpress.Web.v14.1, Version=14.1.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"/><addname="ErrorLog"type="Elmah.ErrorLogModule, Elmah"preCondition="managedHandler"/><addname="ErrorMail"type="Elmah.ErrorMailModule, Elmah"preCondition="managedHandler"/><addname="ErrorFilter"type="Elmah.ErrorFilterModule, Elmah"preCondition="managedHandler"/></modules></system.webServer>

Add a <location> for the path elmah.axd (alongside the existing <location> tags).

web.config
123456789101112131415161718192021
<locationpath="elmah.axd"inheritInChildApplications="false"><system.web><httpHandlers><addverb="POST,GET,HEAD"path="elmah.axd"type="Elmah.ErrorLogPageFactory, Elmah"/></httpHandlers><!--         See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for         more information on using ASP.NET authorization securing ELMAH.<authorization><allow roles="admin" /><deny users="*" />  </authorization>      --></system.web><system.webServer><handlers><addname="ELMAH"verb="POST,GET,HEAD"path="elmah.axd"type="Elmah.ErrorLogPageFactory, Elmah"preCondition="integratedMode"/></handlers></system.webServer></location>

Add a new <elmah> section. I put mine just before the final </configuration> tag.

web.config
12345678
<elmah><errorLogtype="Elmah.XmlFileErrorLog, Elmah"logPath="~/App_Data/Elmah.Errors"/><!--        See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for         more information on remote access and securing ELMAH.    --><securityallowRemoteAccess="false"/></elmah>

Now modify HttpModules.Web.Config to look like this:

HttpModules.Web.Config
1234567
<?xml version="1.0" encoding="utf-8"?><httpModules><addname="ASPxHttpHandlerModule"type="DevExpress.Web.ASPxClasses.ASPxHttpHandlerModule, DevExpress.Web.v14.1, Version=14.1.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"/><addname="ErrorLog"type="Elmah.ErrorLogModule, Elmah"/><addname="ErrorMail"type="Elmah.ErrorMailModule, Elmah"/><addname="ErrorFilter"type="Elmah.ErrorFilterModule, Elmah"/></httpModules>

Now we need to extend XAF’s standard error handling. Create a new class in the web application.

12345678910
publicclassElmahErrorHandling:ErrorHandling{protectedoverridevoidLogException(ErrorInfoerrorInfo){base.LogException(errorInfo);if(errorInfo.Exception!=null)Elmah.ErrorSignal.FromCurrentContext().Raise(errorInfo.Exception);}}

And then modify Global.asax.cs to instantiate the new class

1234567
protectedvoidApplication_Start(objectsender,EventArgse){ErrorHandling.Instance=newElmahErrorHandling();// <---this line is newASPxWebControl.CallbackError+=newEventHandler(Application_Error);#if DEBUGTestScriptsManager.EasyTestEnabled=true;#endif}

The complete files are available with the source code.

Now run the application and trigger an unhandled exception. Change the URL to something that does not exist. Or open any detail view and modify the URL so that the Guid in the ShortcutObjectKey is invalid (replace a digit with an ‘X’). Then the application error page appears.

Then return to the application and change the URL to Elmah.axd. You are looking at the log of all unhandled exceptions.

And for each exception, you can view the full details of any logged exception including coloured stack trace and full server variables.

ELMAH options

By default, ELMAH is configured to disallow remote access to the error logs - only a local user can get to elmah.axd. If you take care of the security implications it can be very useful to enable remote access and monitor the logs on your production servers.

We chose to use an XML file for each error but ELMAH is entirely pluggable. There are dozens of alternatives for persisting the error log including Sql Server, an RSS feeds, to Twitter, even to an iPhone app. There are even third party sites such as elmah.io who will host your error logs for you.

One of the advantages of using XML files is that the files can be copied to another machine. If you look in MainDemo.Web\App_Data\Elmah.Errors, you will find the resulting xml files.

You can just copy these files to another installation’s Elmah.Errors folder and the log will show up when you visit Elmah.axd.

One final note. ELMAH was developed for ASP.NET applications and web services, but it is possible to get it to work with other types of applications such as Windows Forms, Windows Service or console applications. Check out this StackOverflow question.

The source code for this example is on GitHub.


Viewing all articles
Browse latest Browse all 861

Trending Articles