2013-05-27 32 views
15

你好,我已经安装ELMAH在我的项目,但我得到了很多错误,如ELMAH日志怎么按类型忽略错误

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

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

System.Web.HttpException(0X80004005):从客户端(:)中检测到潜在危险的 Request的值。在 System.Web.HttpRequest.ValidateInputIfRequiredByConfig()在 System.Web.HttpApplication.PipelineStepManager.ValidateHelper(HttpContext的背景下 )

我想忽略他们,不要发到我的邮箱,但写在ELMAH DB中。 可以做吗?

回答

19

是的,你可以在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> 

第一<regex>条件filters based on the filtering source,从而不会发生邮件。 Check out the documentation on the wiki for full details

+0

我不能让它停止发送“System.InvalidOperationException:请求的资源只能通过SSL访问。”我的模式是: <等于结合= “HttpStatusCode” 值= “404” 型= “的Int32”/> <正则表达式结合= “FilterSourceType.Name” 图案= “邮件”/>

+1

@Rysystephens你应该有一个''节点,所以第二个被忽略。您需要将''与您的404&消息模式案例一起,然后''与邮件源案例一起使用。您为什么不在SO或ELMAH讨论组(http://groups.google.com/group/elmah)上发布这个问题作为另一个问题,我们可以从那里拿走它? –

14

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

这与问题在我的代码库中提出的问题相反。我仍然发送电子邮件,但不记录到数据库。 – Marc

+0

正是我在找的,谢谢。 – webnoob

+1

有两个功能,一个用于从日志中过滤,另一个用于过滤电子邮件。根据https://code.google.com/p/elmah/wiki/ErrorFiltering – AaronM