diff --git a/CHANGELOG.md b/CHANGELOG.md index ff90e1e..22969fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,14 @@ Changelog ### Bug fixes -* Handle any exceptions raised when reading files for code segments +* Handle any exceptions raised when reading files for code segments. | [twometresteve](https://github.com/twometresteve) | [#123](https://github.com/bugsnag/bugsnag-dotnet/pull/123) +* Account for process termination behavior when handling UnobservedTaskExceptions. + | [twometresteve](https://github.com/twometresteve) + | [#125](https://github.com/bugsnag/bugsnag-dotnet/pull/125) + ## 2.2.0 (2018-07-19) ### Enhancements diff --git a/src/Bugsnag/Bugsnag.csproj b/src/Bugsnag/Bugsnag.csproj index c3f9b7a..509d649 100644 --- a/src/Bugsnag/Bugsnag.csproj +++ b/src/Bugsnag/Bugsnag.csproj @@ -1,4 +1,4 @@ - + Bugsnag Bugsnag .NET Notifier @@ -19,6 +19,8 @@ + + diff --git a/src/Bugsnag/UnhandledException.cs b/src/Bugsnag/UnhandledException.cs index a36452f..d884f29 100644 --- a/src/Bugsnag/UnhandledException.cs +++ b/src/Bugsnag/UnhandledException.cs @@ -11,9 +11,11 @@ class UnhandledException private readonly object _currentClientLock = new object(); private IClient _currentClient; + private bool _unobservedTerminates; private UnhandledException() { + _unobservedTerminates = DetermineUnobservedTerminates(); AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit; TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; @@ -46,6 +48,26 @@ public void ConfigureClient(IClient client, IConfiguration configuration) } } + /// + /// Determines if an UnobservedTaskException leads to the process terminating, based on the target + /// framework and (when applicable) configuration. + /// + /// + private bool DetermineUnobservedTerminates() + { +#if NET35 || NET40 + return true; +#elif NET45 + System.Xml.Linq.XElement configFile = System.Xml.Linq.XElement.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); + var configValue = configFile?.Element("runtime")?.Element("ThrowUnobservedTaskExceptions")?.Attribute("enabled")?.Value; + bool value; + var success = bool.TryParse(configValue, out value); + return success && value; +#else //NETSTANDARD1_3 || NETSTANDARD2_0 + return false; +#endif + } + private void CurrentDomain_ProcessExit(object sender, EventArgs e) { HandleEvent(null, true); @@ -53,7 +75,7 @@ private void CurrentDomain_ProcessExit(object sender, EventArgs e) private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) { - HandleEvent(e.Exception as Exception, !e.Observed); + HandleEvent(e.Exception as Exception, _unobservedTerminates && !e.Observed); } [HandleProcessCorruptedStateExceptions]