Handle exceptions in web services with Elmah

ASP.NET web services never fire Application_Error event and exceptions cannot be handled globally by ELMAH like in ASP.NET apps. But we can “manually” log exceptions using ELMAH: public int WebServiceMethod() { try { … } catch (Exception ex) { Elmah.ErrorLog.GetDefault( HttpContext.Current).Log(new Elmah.Error(ex, HttpContext.Current)); } }

ELMAH – MVC 3 – 403 – Forbidden: Access is denied

(This is all from the documentation/getting started) You don’t need the following line: routes.IgnoreRoute(“elmah.axd”); The next line takes care of it. Everything you need to configure is in your web.config file. Something like: <elmah> <security allowRemoteAccess=”yes” /> <errorLog type=”Elmah.SqlErrorLog, Elmah” connectionStringName=”mySqlConnString” /> </elmah> <location path=”elmah.axd”> <system.web> <authorization> <allow roles=”Administrator” /> <deny users=”*” /> </authorization> </system.web> … Read more

How to set up Elmah with ASP.NET Web API

There are two options by using ELMAH to capture exceptions in WEB API. If you want to capture errors that are encountered in Actions and Controllers , i.e. in your business logic you can create a ActionFilterAttribute and log those exceptions to ELMAH. example: public class UnhandledExceptionFilter : ExceptionFilterAttribute { public override void OnException(HttpActionExecutedContext context) … Read more

Where does ELMAH save its data?

Yes, by default it uses memory storage. When your application pool is restarted, you loose elmah data. If I remember well, old versions of elmah used App_Data folder for storing xml files…If you want to use database to store logs, just specify connection string in your elmah config section: <elmah> … <errorLog type=”Elmah.SqlErrorLog, Elmah” connectionStringName=”ElmahConnectionString”/> … Read more

How to secure Elmah.axd?

The typical scenario for securing elmah.axd is allowing only some authenticated user to be able to access it. But if your site doesn’t use any authentication at all this might not be applicable. Here’s what I would recommend you: Disable completely the elmah.axd handler on your main site Configure elmah to write the logs to … Read more