简体   繁体   English

具有CreateErrorResponse的WebAPI(OWIN)全局异常记录

[英]WebAPI(OWIN) Global Exception Logging with CreateErrorResponse

Is there anyway to capture handled exceptions when CreateErrorResponse() is used to return Error response with API methods? 无论如何,当使用CreateErrorResponse()返回API方法的错误响应时,是否捕获了处理过的异常? I currently have registered a simple global exception loggger for any exceptions, however for some api responses including Model Filter attribute, I'm CreateErrorResponse() to return error responses but this doesn't to enter the Log() method in the Global Exception Logger. 我目前已为任何异常注册了一个简单的全局异常Loggger,但是对于某些API响应(包括模型过滤器属性),我是CreateErrorResponse()以返回错误响应,但这并未在Global Exception Logger中输入Log()方法。 Example usage of CreateErrorResponse in an API method : API方法中CreateErrorResponse的用法示例:

return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);

Global Exception Logger: 全局异常记录器:

public class GlobalExceptionLogger : ExceptionLogger
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public override void Log(ExceptionLoggerContext context)
        {
            log.Error(context.Exception);
        }
    }

The only way to get Log() method to be called is rather than use CreateErrorResponse, throw an Exception instead. 获取Log()方法的唯一方法是使用CreateErrorResponse而不是抛出Exception。 Is this correct? 这个对吗?

You should log the error and any trace info in the same negative flow where you are constructing and returning the Error Response. 您应该将错误和任何跟踪信息记录在构造和返回错误响应的相同否定流程中。

Using exceptions to control application flow is bad practice, avoid it. 使用异常来控制应用程序流是不好的做法,请避免使用它。

The global handlers use is to handle anything that you haven't already handled and should be for Very exceptional cases, you also log there and present the user with a generic error message, while you sort out the chaos in the back of course :) 全局处理程序的用途是处理您尚未处理的任何情况,在非常特殊的情况下,应该处理所有尚未处理的事情,您还可以在此处登录并向用户显示一般错误消息,同时理清后面的混乱情况:)

If I misunderstood your question please correct me. 如果我误解了您的问题,请纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM