-
Notifications
You must be signed in to change notification settings - Fork 4
Logging Unhandled Exceptions
Rolf Kristensen edited this page Oct 15, 2023
·
1 revision
The MAUI platform have failed to provide an unified way of tracking unhandled exceptions.
The normal way would be to hook into AppDomain.CurrentDomain.UnhandledException
, but extra logic needed for iOS and Android:
Exceptions on iOS flows through AppDomain.CurrentDomain.UnhandledException, but one must set UnwindNativeCode:
ObjCRuntime.Runtime.MarshalManagedException += (_, args) =>
{
args.ExceptionMode = ObjCRuntime.MarshalManagedExceptionMode.UnwindNativeCode;
};
Exceptions on Android only flows through Android.Runtime.AndroidEnvironment.UnhandledExceptionRaiser
:
Android.Runtime.AndroidEnvironment.UnhandledExceptionRaiser += (sender, args) =>
{
// Log unhandled args.Exception
};
NLog.Targets.MauiLog includes unified method to setup UnhandledExceptionEventHandler
:
NLog.LogManager.Setup().RegisterMauiLog((sender, ex) => {
NLog.LogManager.GetLogger("Application").Fatal(ex, "Unhandled Exception");
});
One can also consider monitoring unobserved task exceptions:
TaskScheduler.UnobservedTaskException += (sender, args) => {
NLog.LogManager.GetLogger("Application").Error(args.Exception, "Unobserved Task Exception");
};