Filter on exception text in elmah

2019-02-07 12:47发布

问题:

Is there a way to filter exceptions in elma using the exception message?

Examples:
"System.Web.HttpException: Request timed out." I don't want to filter out all HttpException, but only the timed-out requests.
"System.Web.HttpException: Maximum request length exceeded."

What I don't want to do is write own code for that. So is it possible to do this with the buildin-web.config configuration?

Thank you!

回答1:

Yes you can. Just use a regular expression to interrogate the message. See the example below for details on how to compare the exception message.

<errorFilter>
  <test>
    <!-- http://groups.google.com/group/elmah/t/cbe82cee76cc6321 -->
    <and>
      <is-type binding='Exception'
               type='System.Web.HttpException, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' />
      <regex binding='Exception.Message'
             pattern='invalid\s+viewstate'
             caseSensitive='false' />
      <regex binding='Context.Request.UserAgent'
             pattern='Trident/4(\.[0-9])*'
             caseSensitive='false' />
    </and>
  </test>
</errorFilter>


回答2:

You can set up an event handler in your global.asax to avoid ugly configuration regex settings:

void ErrorMail_Filtering(object sender, Elmah.ExceptionFilterEventArgs e) 
{     
    if (e.Exception.Message.Contains("Request timed out"))
        e.Dismiss(); 
}

See Error Filtering.