views:

56

answers:

1

I'm using Elmah to log exceptions for my website, everything seems to be all working fine until one day I noticed that the 500 server errors are not being caught properly. I'm using following script to specifically ignore errors from ScriptResource.axd file.

<errorFilter>
        <test>
            <or>
                <and>
                    <regex binding="FilterSourceType.Name" pattern="mail" />
                    <jscript>
                        <expression>
                            <![CDATA[
                            // @assembly mscorlib
                            // @assembly System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
                            // @import System.IO
                            // @import System.Web 
                            (Context.Request.ServerVariables["URL"].match(/ScriptResource\.axd/i) && BaseException instanceof System.FormatException)
                            ]]>
                        </expression>
                    </jscript>
                </and>
            </or>
        </test>

It seems to be working fine when the first error is triggered. However, the next time this error is triggered Elmah stopped filtering and fails to send email. I was able to reproduce this problem locally and here's the source of the problem:

Microsoft.JScript.JScriptException: Object reference not set to an instance of an object. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Web.HttpServerVarsCollection.Get(String name)
   at invoker2.Invoke(Object , Object[] )
   at Microsoft.JScript.JSMethodInfo.Invoke(Object obj, BindingFlags options, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.JScript.LateBinding.CallOneOfTheMembers(MemberInfo[] members, Object[] arguments, Boolean construct, Object thisob, Binder binder, CultureInfo culture, String[] namedParameters, VsaEngine engine, Boolean& memberCalled)
   at Microsoft.JScript.LateBinding.Call(Binder binder, Object[] arguments, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters, Boolean construct, Boolean brackets, VsaEngine engine)
   at Microsoft.JScript.LateBinding.Call(Object[] arguments, Boolean construct, Boolean brackets, VsaEngine engine)
   at Microsoft.JScript.Call.Evaluate()
   --- End of inner exception stack trace ---
   at Microsoft.JScript.Block.Evaluate()
   at Microsoft.JScript.FunctionObject.CallASTFunc(Object[] args, Object thisob, ScriptObject enclosing_scope, Closure calleeClosure, Binder binder, CultureInfo culture)
   at Microsoft.JScript.FunctionObject.Call(Object[] args, Object thisob, ScriptObject enclosing_scope, Closure calleeClosure, Binder binder, CultureInfo culture)
   at Microsoft.JScript.Closure.Call(Object[] args, Object thisob, Binder binder, CultureInfo culture)
   at Microsoft.JScript.LateBinding.CallValue(Object val, Object[] arguments, Boolean construct, Boolean brackets, VsaEngine engine, Object thisob, Binder binder, CultureInfo culture, String[] namedParameters)
   at Microsoft.JScript.LateBinding.CallValue(Object thisob, Object val, Object[] arguments, Boolean construct, Boolean brackets, VsaEngine engine)
   at Elmah.Assertions.JScriptAssertion.FullTrustEvaluationStrategy.Eval(Object context) in C:\workspace\v2_psp\Elmah\src\Elmah\Assertions\JScriptAssertion.cs:line 312

What I don't understand is how and why this occurs. I also tried other ServerVariables and so far I found out that HTTPS, HTTP_REFERER will NOT trigger this error when the same exception happen the second time. While URL, SCRIPT_NAME, PATH_INFO, PATH_TRANSLATED WILL trigger this error.

Thoughts?

A: 

Okay! After further digging, it seems like you will NOT be able to access any of the DynamicServerVariable from ServerVariables["XXX"] when using Elmah's JScriptAssertion. However, I did found a workaround, seems like these DynamicServerVariable have public properties mapping on the HttpRequest object!!

Therefore, in my case where I'd like to access Context.Request.ServerVariables["URL"], I can replace the code with Context.Request.FilePath, which is how "URL" is mapped to in the HttpRequest object. This way, the JScriptAssertion would not throw exception when the exception is thrown the second time around.

Following are DynamicServerVariable and their properties mapping in HttpRequest.

ServerVariables     HttpRequest Property
URL                 FilePath
SCRIPT_NAME         FilePath (Same as URL)
PATH_INFO           Path
PATH_TRANSLATED     PhysicalPathInternal
QUERY_STRING        QueryStringText
AUTH_TYPE           if (_context.User != null && _context.User.Identity.IsAuthenticated) Context.User.Identity.AuthenticationType, otherwise String.empy
AUTH_USER           if (_context.User != null && _context.User.Identity.IsAuthenticated) Context.User.Identity.Name, otherwise String.empty
REMOTE_USER         if (_context.User != null && _context.User.Identity.IsAuthenticated) Context.User.Identity.Name, otherwise String.empty (Same as AUTH_USER)
BlueFox