【问题标题】:Filter on exception text in elmah过滤elmah中的异常文本
【发布时间】:2010-12-19 19:43:18
【问题描述】:
有没有办法使用异常消息过滤 elma 中的异常?
示例:
“System.Web.HttpException:请求超时。”我不想过滤掉所有的 HttpException,而只过滤掉超时的请求。
“System.Web.HttpException:超出最大请求长度。”
我不想为此编写自己的代码。那么是否可以通过 buildin-web.config 配置来做到这一点?
谢谢!
【问题讨论】:
标签:
exception-handling
error-handling
web-config
elmah
【解决方案1】:
是的,你可以。只需使用正则表达式来询问消息。有关如何比较异常消息的详细信息,请参阅下面的示例。
<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】:
您可以在 global.asax 中设置事件处理程序以避免丑陋的配置正则表达式设置:
void ErrorMail_Filtering(object sender, Elmah.ExceptionFilterEventArgs e)
{
if (e.Exception.Message.Contains("Request timed out"))
e.Dismiss();
}
见Error Filtering。