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

Arb*_*æde 15 asp.net elmah

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

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

生成:太阳,2013年5月26日21:46:30 GMT

System.Web.HttpException(0x80004005):从客户端(:)检测到潜在危险的Request.Path值.System.Web.HttpApplication.PipelineStepManager.ValidateHelper(HttpContext context)中的System.Web.HttpRequest.ValidateInputIfRequiredByConfig()处于

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

Ati*_*ziz 20

是的,您可以使用ELMAH中的错误过滤来执行此操作,并在项目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>
Run Code Online (Sandbox Code Playgroud)

第一个<regex>条件基于过滤源过滤,以便不会发生邮件.查看Wiki上的文档以获取完整的详细信息.


Owe*_*wen 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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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();
}
Run Code Online (Sandbox Code Playgroud)