Thursday, March 01, 2012

Get the last ELMAH error

I've quickly become quite fond of using ELMAH for handling errors in ASP.NET web apps.  There is no better (i.e. custom or otherwise) way that I've found.

I updated an application that was using a messy custom error logging mechanism to use ELMAH and it's already saved me time in tracking down problems while updating the app.  The app has a custom errors page that shows a few error details to help the user communicate their problem.  It was displaying the old mechanism's error ID so we could cross reference it in the error log database.  I needed to replace this with the ELMAH version of the same thing.  It turns out to be simple, after I figured out which class to use:

System.Collections.IList errorList = 
    new System.Collections.ArrayList();
Elmah.ErrorLog.GetDefault(this.Context)
   .GetErrors(0, 1, errorList);

if(errorList.Count > 0)
{
 Elmah.ErrorLogEntry entry = 
             errorList[0] as Elmah.ErrorLogEntry;
 // do what you like with 'entry'
}

1 comment:

Daniele Ferla said...

Thanks for solution!