Elmah Error Logging without HttpContext

For those that use or have used Elmah for error handling, you may have gotten quite use to its logging modules and handlers. If you are not quite sure what Elmah is or what it can do…. you should probably check it out: http://code.google.com/p/elmah/

However, Elmah is a pure ASP.NET architecture in the sense that you are going to need an HttpContext to log an error. 9 times out of 10, that’s not a problem at all. That is until you find yourself inside something like an ASP.NET Web hosted endpoint for a WCF Service, in which you have no HttpContext what so ever, even though you are in a web application. Though, the environment and everything else is a perfect scenario for Elmah usage. Well…. check out how you can log to Elmah without an HttpContext:

  1. Elmah.ErrorLog.GetDefault(null).Log(new Error(ex));


–          The GetDefault method takes an HttpContext, however, you can just get around that by passing “null”. It won’t cause an error. You just won’t see HttpContext related information in the error details.

–          The “Log” method requires an object of type “Error” (which is an Elmah error). You can easily create an Error object by passing a “System.Exception” object into the constructor.

Now you are good to go!

Leave a Reply