ELMAH记录如何按类型忽略错误

时间:2013-05-27 04:26:37

标签: asp.net elmah

你好我在我的项目中设置了ELMAH,但是我遇到了很多错误,比如

  

System.Web.HttpException:一个潜在危险的Request.Path值   从客户端检测到(:)。

     

Generated:Sun,26 May 2013 21:46:30 GMT

     

System.Web.HttpException(0x80004005):有潜在危险   从客户端(:)检测到Request.Path值。在   System.Web.HttpRequest.ValidateInputIfRequiredByConfig()at   System.Web.HttpApplication.PipelineStepManager.ValidateHelper(HttpContext的   上下文中)

我想忽略它们,不要发送到我的邮件但是要写在ELMAH DB中。 有可能吗?

2 个答案:

答案 0 :(得分:20)

是的,您可以使用ELMAH中的error filteringdescribed in detail on the project wiki来执行此操作。简而言之,web.config中的以下过滤器应该完成这项工作(假设您已经设置了模块配置部分):

<errorFilter>
    <test>
        <and>
            <regex binding="FilterSourceType.Name" pattern="mail" />
            <regex binding="Exception.Message" 
               pattern="(?ix: \b potentially \b.+?\b dangerous \b.+?\b value \b.+?\b detected \b.+?\b client \b )" />
        </and>
    </test>
</errorFilter>

第一个<regex>条件filters based on the filtering source,以便不会发送邮件。 Check out the documentation on the wiki for full details

答案 1 :(得分:16)

不涉及正则表达式的解决方案,只需将其添加到Global.asax.cs:

protected void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e)
{
    if (e.Message == "A potentially dangerous Request.Path value was detected from the client (:).")
        e.Dismiss();
}

// this method may also be useful
protected void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
    if (e.Message == "A potentially dangerous Request.Path value was detected from the client (:).")
    {
        // do something
    }
}

或者您可以将两种方法结合起来:

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs args)
{
    Filter(args);
}

void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs args)
{
    Filter(args);
}

void Filter(ExceptionFilterEventArgs args)
{
    if (args.Exception.GetBaseException() is HttpRequestValidationException)
        args.Dismiss();
}