Error Filtering Examples

Do you have a handy error fitler configuration that you have used time and time again? Why not share it? Drop it as a comment to this wiki and we’ll add it here.

To learn more about how to filter harmless or noisy errors from your logs, see “Error Filtering”.


Filter requests for favicon.ico that end in a 404 status code:

<errorFilter>
    <test>
        <and>
            <equal binding="HttpStatusCode" 
                   value="404" type="Int32" />
            <regex binding="Context.Request.ServerVariables['URL']" 
                   pattern="/favicon\.ico(\z|\?)" />
        </and>
    </test>
</errorFilter>

Filter requests ending in 404 or those (using a jscript assertion) where where the last raised VB error was a negative number:

<errorFilter> 
  <test> 
    <or> 
      <equal binding="HttpStatusCode" value="404" type="Int32" /> 
      <jscript> 
        <expression> 
        <![CDATA[ 
        // @assembly Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 
        // @import Microsoft.VisualBasic 
        Information.Err().Number < 0 
        ]]> 
        </expression> 
      </jscript> 
    </or> 
  </test> 
</errorFilter>

For background on the above filter, see the “Err.raise, how do I filter out custom error numbers…” thread in the discussion group.


Filters all exceptions that occur on local requests:

<errorFilter>
    <test>
        <equal binding="Context.Request.IsLocal" 
               value="True" type="Boolean" />
    </test>
</errorFilter>

Filter exceptions where the exception message contains certain words. In the example below, the words must be potentially, dangerous, value, detected and client and in the mentioned sequence. This will filter an exception where the message reads “A potentially dangerous Request.Path value was detected from the client”.

<errorFilter>
    <test>
        <regex binding="Exception.Message" 
               pattern="(?ix: \b potentially \b.+?\b dangerous \b.+?\b value \b.+?\b detected \b.+?\b client \b )" />
    </test>
</errorFilter>

This example was created in response to the “Filtering HttpException: A potentially dangerous Request.Path value was detected…” thread in the discussion group.


Filter exceptions of type FileNotFoundException only when an image is requested (request path ends in .jpg, .gif or .png). In addition, filters exceptions resulting from compilation or parsing errors when the request is from local host and the application has debugging enabled (typical when developing dynamically compiled pages with in-line code).

<errorFilter>
    <test>
        <jscript>
            <expression><![CDATA[
            // @assembly mscorlib
            // @assembly System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
            // @import System.IO
            // @import System.Web
            
            // 404s for images
            
            (($.HttpStatusCode == 404 
              || $.BaseException instanceof FileNotFoundException) 
             && $.Context.Request.Path.match(/\.(jpg|png|gif)$/i))
            
            // Compilation errors during development
            
            || ($.Context.IsDebuggingEnabled
                && $.Context.Request.IsLocal 
                && ($.BaseException instanceof HttpCompileException
                    || $.BaseException instanceof HttpParseException))
            ]]></expression>
        </jscript>
    </test>
</errorFilter>

Filter exceptions of type TaskCanceledException but only when the HTTP request method is HEAD and the user agent string contains the text “Microsoft Office Existence Discovery”.

<errorFilter>
    <test>
        <jscript>
            <expression><![CDATA[
            // @assembly mscorlib
            // @import System.Threading.Tasks
            
            // HEAD requests from Microsoft Excel that may cause
            // TaskCanceledException to be raised
                     
            ($.BaseException instanceof TaskCanceledException
             && 'HEAD' === $.Context.Request.HttpMethod
             && $.Context.Request.UserAgent.match(/\bMicrosoft\s+Office\s+Existence\s+Discovery\b/i))
            ]]></expression>
        </jscript>
    </test>
</errorFilter>

This error typically occurs when Microsoft Excel is setup with a Web Query to asynchronous request handler in your application.