How can I use jscript assertion to filter exception types in Elmah 1.1 RC?
-
06-09-2019 - |
Question
The first binding against 404 works so I got the ErrorFilterModule set up properly, but the jscript section does not seems to work at all. HttpRequestValidationException is still being send in the report mail.
<errorFilter>
<test>
<equal binding="HttpStatusCode" value="404" type="Int32" />
<jscript>
<![CDATA[
// @assembly mscorlib
// @assembly System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// @import System.IO
// @import System.Web
HttpStatusCode == 404
|| BaseException instanceof FileNotFoundException
|| BaseException instanceof HttpRequestValidationException
|| BaseException instanceof HttpException
/* Using RegExp below (see http://msdn.microsoft.com/en-us/library/h6e2eb7w.aspx) */
|| Context.Request.UserAgent.match(/crawler/i)
|| Context.Request.ServerVariables['REMOTE_ADDR'] == '127.0.0.1' // IPv4 only
]]>
</jscript>
</test>
</errorFilter>
Solution
When you have more than one condition, the way you have it in your example (<equal>
then <jscript>
), you need to tell ELMAH to either AND them or OR them. The solution is to use <and>
or <or>
, depending on how you want to logically combine the conditions. Below, I have OR-ed the two so either the condition may apply:
<errorFilter>
<test>
<or>
<equal binding="HttpStatusCode" value="404" type="Int32" />
<jscript>
<expression><![CDATA[
// @assembly mscorlib
// @assembly System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// @import System.IO
// @import System.Web
HttpStatusCode == 404
|| BaseException instanceof FileNotFoundException
|| BaseException instanceof HttpRequestValidationException
|| BaseException instanceof HttpException
/* Using RegExp below (see http://msdn.microsoft.com/en-us/library/h6e2eb7w.aspx) */
|| Context.Request.UserAgent.match(/crawler/i)
|| Context.Request.ServerVariables['REMOTE_ADDR'] == '127.0.0.1' // IPv4 only
]]></expression>
</jscript>
</or>
</test>
</errorFilter>
When you have multiple conditions directly under <test>
with no logical (<and>
or <or>
) combination then only the first one is used, which is why your <jscript>
one was being neglected.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow