How to proper log every exception using OWIN

前端 未结 1 1748
失恋的感觉
失恋的感觉 2021-02-05 09:04

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.

相关标签:
1条回答
  • 2021-02-05 09:51

    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

    0 讨论(0)
提交回复
热议问题