我有一个位于控制器顶部的自定义错误属性:
public class JsonExceptionAttribute : HandleErrorAttribute
{
private ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public override void OnException(ExceptionContext errorContext)
{
if (errorContext.ExceptionHandled) return;
if (!errorContext.Action.Attributes.ContainsType(typeof(DontLogPathAttribute)) { // THIS IS PSEUDOCODE
_log.Error($"Oopsie. {HttpContext.Current.Request.Path}");
}
}
}
在控制器的顶部,我只需添加我的属性:
[JsonException]
public class UserController {
public ActionResult JustAnAction{}
[DontLogPath]
public ActionResult SelfService(string myHash, int userId) {}
}
i通常在错误的情况下记录API-CALL的路径。但是有一些我不想这样做的动作。这些情况有DontLogPathAttribute
(只是一个空属性类(
在异常中(OnException
(,我想找出此属性是否存在。但是在例外情况下,我什至没有找到确定当前操作的属性。
误差日志之前的线是纯伪代码,显示我要实现的目标。可以做到吗?
(在这里我已经看到了几个类似的问题,但是在这种情况下,从来没有一个 exceptionContext 必须处理。(
这应该完成作业
public override void OnException(ExceptionContext errorContext)
{
string actionName = errorContext.RouteData.Values["action"] as string;
MethodInfo method = errorContext.Controller.GetType().GetMethods()
.FirstOrDefault(x => x.DeclaringType == errorContext.Controller.GetType()
&& x.Name == actionName);
IEnumerable<Attribute> attributes = method.GetCustomAttributes();
//DoStuff
}