18

my question should be quite simple, but unfortunately I had no luck in solving it.

Basically, I have some Web API controllers hosted by OWIN and deployed on Azure.

I really need to track down exceptions that occur in each middleware (for example OAuthAuthorizationServerProvider or SignalR Persistent Connections), but I definitely don't have a clue on how to achieve it.

  • I tried Elmah, but it doesn't seem to work properly with OWIN due to lacking HttpContext.
  • I tried using log4net, but I'm only able to log exceptions thrown by Web API Controllers using a custom ExceptionFilterAttribute.. others are ignored.
  • I tried to define a custom LoggerFactory and to assign it in Startup, using app.SetLoggerFactory(new MyLoggerFactory()), but exception thrown by other middlewares are not logged.
  • I tried to get at least a meaningful error message sent to the client, but despite <customErrors mode="Off"/> and <deployment retail="false"/>, Azure refuses to return anything but {"message":"an error has occurred"}.. I tried both Azure Web Sites and Azure Cloud Services.
  • I saw some cloud alternatives that should work with OWIN, like Elmah.io or Raygun.io, but I don't need their cloud features and it is definitely not worth paying hundreds $ per year just to log some exceptions.

What should be the best way to log any possible exception thrown by my application?

Thanks for your help

Menion Leah
  • 341
  • 2
  • 11

1 Answers1

12

have you take a look at this link ? http://www.asp.net/web-api/overview/error-handling/web-api-global-error-handling

Because you can't catch all the exceptions using an exceptionFilter, they propose to use a IExceptionLogger and IExceptionHandler to allow global error handling in Web Api 2.

After that, if it's not fit your need, you can construct an OwinMiddleWare that you will place in first position (before the Authenticate stage), this middleware could :

  1. create a requestId in the header of the response
  2. analyse the response code, before sending response, and if it's not a IsSuccessStatusCode, you could log the exception message to a DB and replace the content of the response to send a simple error message to the client using the requestId (to allow you to find the related exception in your db)

hope this help

  • 2
    Thank you very much, Jeremie. That did the trick. Specifically, using an IExceptionHandler wasn't enough to catch exceptions from all middlewares, including OAuth. But defining a new middleware, and handling exceptions inside it, solved both my problems: logging them and decide what to forward to the client. Thanks again! :) – Menion Leah Dec 07 '14 at 23:51
  • @MenionLeah I am in the same search to capture the exception using middle ware, do you have a recommendation of a tool ? We used elmah in previous application and we want something similar, thanks – Juan Oct 20 '15 at 12:41
  • Hi @Juan; sorry, I ended up using my own code to store them in our database, and provided a webpage to access them. – Menion Leah Oct 20 '15 at 16:24
  • Ok thanks for your time, Im looking serilog they implemented using the ilogger from microsoft and it looks nice the query functionality. – Juan Oct 20 '15 at 17:05
  • 1
    @MenionLeah I'm trying to achieve the same, is it possible to update your answer and share a code snippet on how you implemented this? – Taiseer Joudeh Dec 27 '15 at 06:16