Ashley Lee Ashley Lee - 1 year ago 83
ASP.NET (C#) Question

Exception thrown inside Exceptional logging

I’m replacing Elmah with Exceptional, but having an issue. It works fine if the exception is thrown inside my web project or if the error is a 404 (The controller for path '/blah/blah' was not found or does not implement IController).

But if I throw an exception from within one of my referenced projects that’s not my web project, then Exceptional throws an error when attempting to log. The following error and stacktrace only gets displayed in the console. The error gets swallowed at that point and doesn't get logged anywhere.

One of the beauties of open-source, is that I can see the code that’s throwing the exception, but no idea why…

Error.cs (line 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\\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

I am registering Exceptional like this:

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

And this is the only code in my web.config:

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

Answer Source

I finally found the issue. The code below is what I use to logout a user from my application.

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

When the 3rd line executes and sets the user to a new ClaimsPrinciple, it removes the following 3 values (AUTH_TYPE, AUTH_USER, REMOTE_USER) and sets them to null inside the System.Web.HttpContext.Current.Request.ServerVariables object, even though the keys are still there inside the object.

So any exception thrown after that line is not logged by Stackexchange.Exceptional, as it tries to read those values and then throws a System.NullReferenceException.