简体   繁体   English

WebApi中的异常和日志处理程序

[英]Exception and log handlers in WebApi

Im developing a ASP.NET WebApi using a specialized implementation of the ExceptionLogger and ExceptionHandler but i have some doubts about those two classes. 我使用ExceptionLoggerExceptionHandler的专门实现来开发ASP.NET WebApi,但是我对这两个类有一些疑问。 By using the ExceptionHandler class im returning some custom response message to the client, so in every exception that might occours on the controller or service layer it is handled in this class. 通过使用ExceptionHandler类,im向客户端返回一些自定义响应消息,因此在控制器或服务层上可能发生的每个异常中,都可以在此类中处理。 My first question is, can it replace all the try/catch blocks on the controller? 我的第一个问题是,它可以代替控制器上的所有try/catch块吗? The documentation says that the ExceptionHandler is a global filter, and it always be fired. 该文档说ExceptionHandler是一个全局过滤器,并且始终会被触发。

My other doubt is if its possible to catch the 404(not found) errors within this class. 我的另一个疑问是,是否有可能在此类中捕获404(未找到)错误。 I tried but i couldnt catch it. 我试过了,但是我抓不到。

Thanks. 谢谢。

I suggest you to use Global.asax and Application_Error in order to catch all errors. 我建议您使用Global.asaxApplication_Error来捕获所有错误。 You can't use handler class on this topic, because handler occurs always before enter in your application, you can use handler for formatting output. 您不能在此主题上使用处理程序类,因为处理程序总是在输入应用程序之前发生,因此可以使用处理程序来格式化输出。

link : https://msdn.microsoft.com/en-us/library/ms227675(v=vs.100).aspx 链接: https : //msdn.microsoft.com/zh-cn/library/ms227675(v=vs.100).aspx

But it's recommended to delete your try catch in others classes, except specific cases. 但是建议删除其他类中的try catch,但特殊情况除外。 (it's proper) (正确)

For 404 i suggest you to use web.config and customerror node in configuration , or use filter exception in your c#. 对于404,我建议您在配置中使用web.config和customerror节点,或者在c#中使用filter exception

can it replace all the try/catch blocks on the controller 是否可以替换控制器上的所有try / catch块

Yes, you can. 是的你可以。 But I prefer to add more custom information to the exception and rethrow it so that it has more useful information. 但是我更喜欢为异常添加更多的自定义信息,然后重新抛出该异常,以使其具有更多有用的信息。

404 not found 找不到404

For global unhandled exception , I'm using ELMAH to do the logging. 对于全局unhandled exception ,我正在使用ELMAH进行日志记录。 It also can catch 404 not found and log it. 它还可以捕获404 not found并将其记录下来。

It's easy to config and easy to use 易于配置和易于使用

Link: https://code.google.com/p/elmah/ 链接: https//code.google.com/p/elmah/

If you use IIS to host, you can also customize 404 error page there. 如果使用IIS进行托管,则还可以在其中自定义404错误页面。

I don't put try/catch blocks in controllers. 我不会在控制器中放置try / catch块。 You don't show any code, but as an alternative you may want to try this. 您没有显示任何代码,但是作为替代,您可能想尝试一下。 Use a custom exception attribute that inherits from ExceptionFilterAttribute. 使用从ExceptionFilterAttribute继承的自定义异常属性。 Basically, it handles the exception and returns a HttpResponseException to the client. 基本上,它处理异常并将HttpResponseException返回给客户端。

Here's the attribute: 这是属性:

public class HandleExceptionAttribute : ExceptionFilterAttribute
    {
        private HttpStatusCode _status;
        private Type _type;

        public Type Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public HttpStatusCode Status
        {
            get { return _status; }
            set { _status = value; }
        }

        public override void OnException(HttpActionExecutedContext context)
        {
            var exception = context.Exception;

            if (exception == null || exception.GetType() != Type) return;

            var response = context.Request.CreateErrorResponse(Status, exception.Message, exception);

            throw new HttpResponseException(response);
        }
    }

Here's the usage: 这是用法:

[HandleException(Type = typeof (ArgumentNullException), Status = ttpStatusCode.InternalServerError)]
public class MyController : ApiController
{
   //...
}

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

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