如何美元的一个错误处理的属性$ p ELMAH pvent记录错误 [英] How to prevent Elmah logging errors handled in a Error Attribute
问题描述
我使用了asp.net,mvc4 web应用程序ELMAH和Elmah.mvc包。我有,我想处理 HttpAntiForgeryExceptions
以特定方式特定的控制器动作。我创建了一个自定义的HandleErrorAttribute从 HandleErrorAttribute
继承并实施个IExceptionFilter
。
I am using Elmah and Elmah.mvc packages in a asp.net, mvc4 web app. I have a specific controller action where I want to handle HttpAntiForgeryExceptions
in a specific manner. I have created a custom HandleErrorAttribute that inherits from HandleErrorAttribute
and implements IExceptionFilter
.
在我要处理的具体情况,我设置了 ExceptionContext.ExceptionHandled
为true。用户看到的行为是正确的,错误处理,我希望它是。然而,它也记录一个错误ELMAH,我不希望它做的,因为我想保持ELMAH日志真正的错误。
Under the specific circumstances I want to handle, I set the ExceptionContext.ExceptionHandled
to true. The behaviour that the user sees is correct and the error is handled as I want it to be. However, it also logs an error to Elmah, which I don't want it to do, as I would like to keep the Elmah log for true errors.
控制器注解如下:
[ValidateAntiForgeryToken]
[CustomHandleAntiforgeryError]
public ActionResult ControllerMethod(Model model)
{
...
}
该CustomHandleAntiforgeryError如下:
The CustomHandleAntiforgeryError looks like:
public class CustomHandleAntiforgeryErrorAttribute:
HandleErrorAttribute, IExceptionFilter
{
public override void OnException(ExceptionContext filterContext)
{
if (circumstancesAreOk)
{
filterContext.ExceptionHandled = true;
return;
}
}
}
还有什么我需要做的prevent这个错误被记录与ELMAH?
Is there anything else I need to do to prevent this error being logged with Elmah?
---编辑---
综观Elmah.MVC源<一个href=\"https://github.com/alexanderbeletsky/elmah-mvc/blob/master/src/Elmah.Mvc/HandleErrorAttribute.cs\"相对=nofollow> HandleErrorAttribute 同时记录处理和未处理的错误
Looking at the Elmah.MVC source the HandleErrorAttribute logs both handled and unhandled errors
public override void OnException(ExceptionContext context)
{
base.OnException(context);
if (!context.ExceptionHandled) // if unhandled, will be logged anyhow
return;
var e = context.Exception;
var httpContext = context.HttpContext.ApplicationInstance.Context;
if (httpContext != null &&
(RaiseErrorSignal(e, httpContext) // prefer signaling, if possible
|| IsFiltered(e, httpContext))) // filtered?
return;
LogException(e, httpContext);
}
我想我的自定义内的方式属性来通知ELMAH不会记录这个错误,并会AP preciate任何想法。
I would like a way within my custom attribute to signal to Elmah not to log this error and would appreciate any ideas.
推荐答案
我做了什么来解决这个问题,我认为是丑陋的,但我是经历了奇怪的错误过滤器角落的情况下工作。我添加了一个自定义的HandleErrorAttribute,从ELMAH <复制的href=\"https://github.com/alexanderbeletsky/elmah-mvc/blob/master/src/Elmah.Mvc/HandleErrorAttribute.cs\"相对=nofollow> HandleErrorAttribute 并列入onException的方法空检查。
What I did to solve this I think is ugly, but worked for the weird error filter corner case that I was experiencing. I added a custom HandleErrorAttribute, copied from the Elmah HandleErrorAttribute and included a null check in the OnException method.
public override void OnException(ExceptionContext context)
{
base.OnException(context);
if (!context.ExceptionHandled) // if unhandled, will be logged anyhow
return;
string[] formKeys = context.HttpContext.Request.Form.AllKeys;
var e = context.Exception;
// linked to CustomErrorAttribute
if (e == null)
{
return;
}
bool test = HostingEnvironment.IsHosted;
var httpContext = context.HttpContext.ApplicationInstance.Context;
if (httpContext != null &&
(RaiseErrorSignal(e, httpContext) // prefer signaling, if possible
|| IsFiltered(e, httpContext))) // filtered?
return;
LogException(e, httpContext);
}
然后,在我的错误过滤器,这是我不想触发短信到ELMAH,还有ExceptionContext ExceptionHandled设置为true,我设置的例外为空
Then, in my error filter, which I did not want to trigger messaging to Elmah, as well as setting the ExceptionContext ExceptionHandled to true I set the Exception to null
public class MyCustomErrorFilter : IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (blah, blah, blah, weird things but not something terrible)
{
filterContext.Exception = null;
filterContext.ExceptionHandled = true;
return;
}
}
}
如果这是一件非常棘手且经常发生,我可能会叉ELMAH,并期待在创建自定义ELMAH建设成为本哈克的感觉一点点地依赖于多种情况。
If it was something more involved and a regular occurrence I would probably fork Elmah and look at creating a custom Elmah build as this feels a little bit hacky to rely on in multiple situations.
这篇关于如何美元的一个错误处理的属性$ p ELMAH pvent记录错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!