NickCraver / StackExchange.Exceptional

Error handler used for the Stack Exchange network
https://nickcraver.com/StackExchange.Exceptional/
Apache License 2.0
860 stars 170 forks source link

NullReferenceException when not using Web.config configuration #98

Closed stijnherreman closed 6 years ago

stijnherreman commented 6 years ago

Version: 2.0.0-alpha-129 Environment: ASP.NET Web API 2 using OWIN

When only the module is configured in Web.config, without any other changes to Web.config, the module crashes.

[NullReferenceException: Object reference not set to an instance of an object.]
   StackExchange.Exceptional.ConfigSettings.LoadSettings() in C:\projects\stackexchange-exceptional\src\StackExchange.Exceptional\ConfigSettings.cs:24
   StackExchange.Exceptional.ExceptionalModule..cctor() in C:\projects\stackexchange-exceptional\src\StackExchange.Exceptional\ExceptionalModule.cs:12

[TypeInitializationException: The type initializer for 'StackExchange.Exceptional.ExceptionalModule' threw an exception.]
   StackExchange.Exceptional.ExceptionalModule..ctor() +0

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +119
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +247
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
   System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark) +1088
   System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +128
   System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture) +20
   System.Web.HttpRuntime.CreateNonPublicInstance(Type type, Object[] args) +60
   System.Web.HttpApplication.BuildIntegratedModuleCollection(List`1 moduleList) +191
   System.Web.HttpApplication.GetModuleCollection(IntPtr appContext) +1068
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +82
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +173
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): Exception has been thrown by the target of an invocation.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +10084960
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +95
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

It looks like ConfigSettings.cs expects the <Exceptional>...</Exceptional> section to be present.

stijnherreman commented 6 years ago

I did find out that the module cannot be used with Web API anyway and I had to register an IExceptionLogger, but I assume the above is still an issue for MVC applications.

Additionally, I noticed the first call to Log (in my case this happens in the IExceptionLogger implementation) silently fails due to this issue. Subsequent calls work as expected.

NickCraver commented 6 years ago

Fixed in #107 along with a few others, will be in the next alpha package.

MattJeanes commented 5 years ago

This appears to be a problem again, looking at latest code in https://github.com/NickCraver/StackExchange.Exceptional/blob/cd964906c44c73e6cc120000a18fe7fc29c84e62/src/StackExchange.Exceptional/ConfigSettings.cs#L24-L38

After adding <section name="Exceptional" type="StackExchange.Exceptional.Settings, StackExchange.Exceptional"/> and <Exceptional applicationName="placeholder" /> it works as expected

MattJeanes commented 5 years ago

@NickCraver do you mind reopening this? Happy to create a new issue if you want