Thursday, 19 March 2015

Elmah + ASP.NET WebAPI tip...

I had one of those situations today where you're looking at various blog posts and stack overflow posts and everybody's essentially saying, "it's easy, you just add the NuGet package, issue a couple lines of configuration code, and it works."  But I was scratching my head for awhile until I connected the dots.  So I thought I'd post a small tip here in hopes of saving somebody a minute or two.

So, if you want to add ELMAH to your ASP.NET WebAPI project to log unhandled exceptions, a bit of googling will quickly lead you to the elmah-contrib-webapi project, and helpful posts such as this one and this one, as well as various stack overflow posts.  I tried the stuff recommended there but for some reason couldn't get it working.  Having used ELMAH previously on an MVC project, I knew that the basic steps are:


  • Install a NuGet package
  • Tweak your web.config
  • Point to where you want to log to (on my previous project it was logging to SQL Server, but in this case the client wanted to log to XML files).
If you do all that, then what you get is, when an unhandled exception occurs (which you can simulate by simply throwing a new Exception in C# code), the error gets logged, and you get a handy web page at the URL http://yourwebsite/elmah.axd).

So here's what wasn't entirely obvious about the Elmah Contrib WebAPI instructions in those various posts:  if your web API happens to still live in the context of an MVC site, as is the case on this project, then I found to get the desired behaviour, I actually had to install TWO NuGet packages, one for the web API, and one for the MVC site.  (In our VS solution, the WebApi functionality happens to reside in the context of an MVC project; it's an AngularJS website, but the MVC routing is simply used to go to the index.html page for the single page app).

Anyway, so here's what I did that made it work:

  • Installed the Elmah.Contrib.WebApi package using the NuGet package manager, as described here.  (I also modified the Application_Start method as shown on that same web page).
  • Installed the Elmah.MVC package as described here.
  • Added the following line of code to the WebApiConfig class as described here.

config.Services.Add(typeof(IExceptionLogger), new ElmahExceptionLogger());

  • Modified the web.config to configure Elmah as desired, as described here.  (In my case I used the XML logging).
  • Then, to test, I added a "throw new Exception( )" statement to a method in a WebApi controller.
  • Then, when you're browsing to your WebApi project (say you push the "go" button in VisualStudio to debug, then when the web page comes up, you add "/elmah" onto the end of the URL in the address bar of the browser; at this point Elmah should be working, and you should see the Elmah page.
  • Then I pointed the Angular site to a page where, upon pushing the submit button, it would call that method and throw the exception.
  • Now, you should see the exceptions being displayed on the Elmah page, and (in my case) the XML files that are being written to disk.
Hope this helps someone.  
cheers, dB

5 comments:

  1. Thanks a lot. I was scratching my head too and then I found your blog. It was really helpful. Thanks a lot again.

    ReplyDelete
  2. Glad it was helpful! Thanks for your comment.

    ReplyDelete