【问题标题】:ELMAH log how to ignore error by typeELMAH 记录如何按类型忽略错误
【发布时间】:2013-05-21 21:40:10
【问题描述】:

您好,我已经在我的项目中设置了 ELMAH,但是我遇到了很多错误,例如

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

生成:2013 年 5 月 26 日星期日 21:46:30 GMT

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

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

【问题讨论】:

    标签: asp.net elmah


    【解决方案1】:

    不涉及正则表达式的解决方案,只需将其添加到您的 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();
    }
    

    【讨论】:

    • 这与我的代码库中的问题相反。我仍然发送电子邮件,但没有登录到数据库。
    • 有两个功能,一个用于从日志中过滤,另一个用于过滤电子邮件。需要使用 ErrorMail_Filtering 函数而不是 ErrorLog_Filtering 从电子邮件中删除,根据 code.google.com/p/elmah/wiki/ErrorFiltering
    • 由于某种原因,args.Exception.GetBaseException() is HttpRequestValidationException 的评估结果不正确。所以我建议检查异常的message属性,看是否包含错误。
    【解决方案2】:

    是的,您可以在 ELMAH 中使用 error filtering 执行此操作,即 described 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>
    

    第一个&lt;regex&gt; 条件filters based on the filtering source 使得邮件不会发生。 Check out the documentation on the wiki for full details.

    【讨论】:

    • 我无法让它停止发送“System.InvalidOperationException:请求的资源只能通过 SSL 访问。”我的模式是:
    • @RhysStephens 你应该有一个&lt;test&gt; 节点,所以第二个节点被忽略了。您需要将您的 404 和消息模式案例 &lt;or&gt; 一起,然后将 &lt;and&gt; 与邮件源案例一起。您为什么不将其作为另一个问题发布在 SO 或 ELMAH Discussion Group 上,我们可以从那里获取它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 2016-10-12
    • 2013-04-03
    • 2011-09-19
    • 2011-03-01
    • 2012-03-04
    • 2019-09-21
    相关资源
    最近更新 更多