... my attempt at documenting my adventures as a developer.
I regularly come across, or generally do quite interesting things in my day job, so when I have some time I try and write about them. Hopefully you'll find some of the Articles handy.
As promised, here is my next article regarding another tool I find completely invaluable in my life as a developer, Elmah.
Basically Elmah sites quietly on your site, logging any exceptions (Code based or Web Server, for example, 404) which occur to (in this example) a database. It then provides a nice neat GUI front end to allow you to view the details of these errors, including stack traces.
If you're anything like me, and are tired of conversations which go like this:
You will be happy Elmah exists!
Installation is easy, just follow these steps. Remember, this guide is based around .NET 4.5 MVC 4 and the associated caveats, it may not be exactly what is required for your configuration but should be near as dammit.
Install-Package elmahWe use the package manager as it will handle the installation of any dependencies, and also add the required sections to your web.config (well, almost).
<elmah> <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="YourConnectionStringName" applicationName="YourWebsiteName" /> <security allowRemoteAccess="true" /> </elmah>It's pretty self explanatory, the connection string name must be a valid connection string from the <connectionStrings> section of your web.config, and the application name should be the name you want to log errors for this application against.
<location path="elmah.axd"> <system.web> <httpHandlers> <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" /> </httpHandlers> <authorization> <allow roles="Admin"/> <deny users="*"/> </authorization> </system.web> <system.webServer> <handlers> <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" /> </handlers> </system.webServer> </location>You'll notice here I have added a constraint which means only users which are members of the Admin group will be able to view Elmah.axd
<modules runAllManagedModulesForAllRequests="true"> <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" /> <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" /> <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" /> </modules>
When you use Elmah with Custom Errors redirects, you'll notice that nothing gets logged to elmah. This is because the framework intercepts the error prior to elmah getting hold of it. To get around this we need to add two global filters, the code for them is fairly, simple:
Public Class ElmahHTTPErrorAttribute Inherits System.Web.Http.Filters.ExceptionFilterAttribute Public Overrides Sub OnException(actionExecutedContext As System.Web.Http.Filters.HttpActionExecutedContext) If actionExecutedContext.Exception IsNot Nothing Then Elmah.ErrorSignal.FromCurrentContext().Raise(actionExecutedContext.Exception) End If MyBase.OnException(actionExecutedContext) End Sub End Class Public Class ElmahMVCErrorAttribute Implements IExceptionFilter Public Sub OnException(filterContext As ExceptionContext) Implements IExceptionFilter.OnException If filterContext.Exception IsNot Nothing Then Elmah.ErrorSignal.FromCurrentContext().Raise(filterContext.Exception) End If End Sub End Class
And then you need to modify your FilterConfig.vb. What we're doing here is creating a new section for HTTP filter attributes, which we will call from global.asax later:
Public Class FilterConfig Public Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection) 'Add elmah attribute filters.Add(New ElmahMVCErrorAttribute, 1) 'Add the standing handle error attribute filters.Add(New HandleErrorAttribute(), 2) End Sub Public Shared Sub RegisterHTTPFilters(ByVal filters As System.Web.Http.Filters.HttpFilterCollection) 'Add a http filter attribute filters.Add(New ElmahHTTPErrorAttribute) End Sub End Class
Finally, update Application_Start in your Global.asax.vb to register the new filters:
And that's it, you're all configured and ready to go, if you go to http://yoursite.com/Elmah.axd, you should see a screen similar to the following:
You can drill down further, by clicking details, which will show you the stack trace if one is available.