2013-05-27 30 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