1

我正在使用 Elmah 为我的网站记录异常,一切似乎都运行良好,直到有一天我注意到 500 个服务器错误没有被正确捕获。我正在使用以下脚本专门忽略 ScriptResource.axd 文件中的错误。

<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>

触发第一个错误时,它似乎工作正常。但是,下次触发此错误时,Elmah 停止过滤并且无法发送电子邮件。我能够在本地重现此问题,这是问题的根源:

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

我不明白这是如何以及为什么会发生的。我还尝试了其他 ServerVariables,到目前为止,我发现HTTPS、HTTP_REFERER在第二次发生相同异常时不会触发此错误。URL、SCRIPT_NAME、PATH_INFO、PATH_TRANSLATED触发此错误

想法?

4

1 回答 1

1

好的!进一步挖掘后,使用 Elmah 的 JScriptAssertion 时,您似乎无法从 ServerVariables["XXX"] 访问任何 DynamicServerVariable。但是,我确实找到了一种解决方法,似乎这些 DynamicServerVariable 在 HttpRequest 对象上有公共属性映射!!

因此,在我想访问 Context.Request.ServerVariables["URL"] 的情况下,我可以将代码替换为 Context.Request.FilePath,这就是“URL”在 HttpRequest 对象中的映射方式。这样,当第二次抛出异常时,JScriptAssertion 不会抛出异常。

以下是 HttpRequest 中的 DynamicServerVariable 及其属性映射。

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)
于 2010-10-02T05:11:23.413 回答