【发布时间】:2015-12-21 21:16:30
【问题描述】:
我有一个 ASP WebAPI 项目。我正在尝试在我的基本控制器上设置一个全局异常处理程序。所以我像这样创建了一个ExceptionFilterAttribute。
using System.Web.Http.Filters;
public class MyExceptionFilterAttribute : ExceptionFilterAttribute
{
protected static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
var exception = actionExecutedContext.Exception;
log.Fatal(exception);
base.OnException(actionExecutedContext);
}
}
然后我也在/App_Start/WebApiConfig.cs注册了
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// ...
// Setup Filters
config.Filters.Add(new MyExceptionFilterAttribute());
}
}
当我将属性添加到我的控制器(或基本控制器)时,没有任何记录。我做错了什么?
编辑:我的控制器抛出异常:
[HttpGet]
public string Hello(string name)
{
if (name.Equals("error", StringComparison.OrdinalIgnoreCase))
{
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
else
{
return name;
}
}
【问题讨论】:
-
你错过了什么类型的异常,过滤器无法处理的情况有很多。
标签: c# asp.net asp.net-web-api exception-handling