We have a Xamarin iOS application that sits on top of a heavily async PCL library. There is a chance that occasionally a Task will fault in the library and not be observed. We have, therefore, wired up the UnobservedTaskException handler on the TaskScheduler to ensure this doesn't necessarily bring our app down.
This worked fine until we enabled HockeyApp for centralising crash reporting. Hockey adds its own handler for unobserved task exceptions which always terminates the app after sending a crash report to its servers. I have no problem with them doing this for the AppDomain UnhandledException handler but I need to stop them killing the app on unobserved task exceptions.
There appears to be a mechanism for installing a custom handler for these exceptions but I cannot see how to install it in the Xamarin iOS Hockey SDK
Here is the code we use to enable Hockey
var manager = BITHockeyManager.SharedHockeyManager;
manager.Configure(APPID);
#if DEBUG
manager.DebugLogEnabled = true;
#endif
manager.StartManager();
Does anyone know how to override HockeyApp's default behaviour for UnobservedTaskExceptions?
The UnobservedTaskException
EventHandler is hard-coded in the StartManager
method with no direct way to remove.
TaskScheduler.UnobservedTaskException += (sender, e) => ThrowExceptionAsNative(e.Exception);
Personally, I build from source, publicly expose ThrowExceptionAsNative
and add configuration methods to include/exclude the default handler when StartManager
is called. This is just like HockeyApp does with the HockeySDK-Windows
api.
Thus when catching an UnobservedTaskException
you have an option to handle it yourself or to throw it as a native exception.
This is much like the original Xamarin native bindings and I do not understand why they did it this way as in the HockeySDK-Windows
code, they removed the default handling of UnobservedTaskException
:
Since .NET 4.5, by default, UnobservedTaskExceptions do no longer cause the app to crash. The SDK has not been adapted for this and still logs these errors and causes the program to exit, though this might not be needed or intended.
Users who wish to continue using the handler, should add calls to
RegisterUnobservedTaskExceptionHandler()
orRegisterDefaultUnobservedTaskExceptionHandler()
after callingConfigure()
.
For a couple of clients who do not want a custom build of HockeySDK.Xamarin
, I do the EventHandler
removal via reflection after the StartManager
call and add in our custom handler. Using this approach you will not have a public ThrowExceptionAsNative
available to throw the exception as a native one if needed, but some more reflection can do it :-/