异常日志记录中抛出异常

时间:2016-08-09 14:59:45

标签: c# asp.net exception stackexchange.exceptional

我用Exceptional取代Elmah,但有问题。如果在我的web项目中抛出异常或错误是404(路径' / blah / blah'的控制器未找到或未实现IController),它可以正常工作。

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

开源的一个优点是,我可以看到抛出异常的代码,但不知道为什么......

  

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

我正在注册Exceptional:

  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>

1 个答案:

答案 0 :(得分:0)

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

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。