异常日志记录内部引发异常

本文关键字:异常 记录 日志 内部 | 更新日期: 2023-09-27 18:01:45

我要用Exceptional替换Elmah,但有一个问题。如果在我的web项目中引发异常,或者错误为404(找不到路径"/blah/blah"的控制器或未实现IController(,则它可以正常工作。

但是,如果我在一个引用的项目中抛出了一个异常,而该项目不是我的web项目,那么Exceptional在尝试登录时会抛出一个错误。以下错误和stacktrace仅显示在控制台中。该错误会在此时被吞噬,并且不会在任何地方被记录。

开源的好处之一是,我可以看到抛出异常的代码,但不知道为什么…

错误.cs(第107、135、126行(

Exception thrown: 'System.ArgumentException' in Cms.Services.dll
'w3wp.exe' (CLR v4.0.30319: /LM/W3SVC/1/ROOT/wssp-18-131152201224510365): Loaded 'C:'Windows'assembly'GAC_MSIL'Microsoft.VisualStudio.Debugger.Runtime'14.0.0.0__b03f5f7f11d50a3a'Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Web.HttpRequest.CalcDynamicServerVariable(DynamicServerVariable var)
   at System.Web.HttpServerVarsCollectionEntry.GetValue(HttpRequest request)
   at System.Web.HttpServerVarsCollection.GetServerVar(Object e)
   at System.Web.HttpServerVarsCollection.Get(Int32 index)
   at System.Web.HttpServerVarsCollection.GetValues(Int32 index)
   at System.Collections.Specialized.NameValueCollection.Add(NameValueCollection c)
   at StackExchange.Exceptional.Error.<>c__DisplayClass22_0.<SetContextProperties>b__0(Func`2 getter) in C:'BuildAgent'work'd20fce4a5bb47bd3'StackExchange.Exceptional'Error.cs:line 126
   at StackExchange.Exceptional.Error.SetContextProperties(HttpContext context) in C:'BuildAgent'work'd20fce4a5bb47bd3'StackExchange.Exceptional'Error.cs:line 135
   at StackExchange.Exceptional.Error..ctor(Exception e, HttpContext context, String applicationName) in C:'BuildAgent'work'd20fce4a5bb47bd3'StackExchange.Exceptional'Error.cs:line 107
   at StackExchange.Exceptional.ErrorStore.LogException(Exception ex, HttpContext context, Boolean appendFullStackTrace, Boolean rollupPerServer, Dictionary`2 customData, String applicationName) in C:'BuildAgent'work'd20fce4a5bb47bd3'StackExchange.Exceptional'ErrorStore.cs:line 611

我正在注册这样的例外:

  string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyApp"].ConnectionString;
  ErrorStore.Setup("MyApp", new SQLErrorStore(connectionString));

这是我的web.config中唯一的代码:

<modules runAllManagedModulesForAllRequests="true">
  <add name="ErrorStore" type="StackExchange.Exceptional.ExceptionalModule, StackExchange.Exceptional" />
</modules>

异常日志记录内部引发异常

我终于发现了这个问题。下面的代码是我用来从应用程序注销用户的代码。

var authenticationManager = HttpContext.GetOwinContext().Authentication;
authenticationManager.SignOut();
authenticationManager.User = new ClaimsPrincipal();

当第3行执行并将用户设置为新的ClaimsPrinciple时,它会删除以下3个值(AUTH_TYPE、AUTH_user、REMOTE_user(,并在System.Web.HttpContext.Current.Request.ServerVariables对象内将它们设置为null,即使键仍在对象内。

因此,Stackexchange.Exceptional不会记录该行之后引发的任何异常,因为它试图读取这些值,然后引发System.NullReferenceException。