|
| 1 | +// MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2022 Matt Johnson-Pint |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +namespace NLog.Targets.MauiLog |
| 24 | +{ |
| 25 | + using System; |
| 26 | + |
| 27 | + internal static class MauiExceptions |
| 28 | + { |
| 29 | + // We'll route all unhandled exceptions through this one event. |
| 30 | + public static event UnhandledExceptionEventHandler UnhandledException; |
| 31 | + |
| 32 | + static MauiExceptions() |
| 33 | + { |
| 34 | + // This is the normal event expected, and should still be used. |
| 35 | + // It will fire for exceptions from iOS and Mac Catalyst, |
| 36 | + // and for exceptions on background threads from WinUI 3. |
| 37 | + |
| 38 | + AppDomain.CurrentDomain.UnhandledException += (sender, args) => |
| 39 | + { |
| 40 | + UnhandledException?.Invoke(sender, args); |
| 41 | + }; |
| 42 | + |
| 43 | +#if __APPLE__ |
| 44 | + |
| 45 | + // For iOS and Mac Catalyst |
| 46 | + // Exceptions will flow through AppDomain.CurrentDomain.UnhandledException, |
| 47 | + // but we need to set UnwindNativeCode to get it to work correctly. |
| 48 | + // |
| 49 | + // See: https://github.com/xamarin/xamarin-macios/issues/15252 |
| 50 | + |
| 51 | + ObjCRuntime.Runtime.MarshalManagedException += (_, args) => |
| 52 | + { |
| 53 | + args.ExceptionMode = ObjCRuntime.MarshalManagedExceptionMode.UnwindNativeCode; |
| 54 | + }; |
| 55 | + |
| 56 | +#elif __ANDROID__ |
| 57 | + |
| 58 | + // For Android: |
| 59 | + // All exceptions will flow through Android.Runtime.AndroidEnvironment.UnhandledExceptionRaiser, |
| 60 | + // and NOT through AppDomain.CurrentDomain.UnhandledException |
| 61 | + |
| 62 | + Android.Runtime.AndroidEnvironment.UnhandledExceptionRaiser += (sender, args) => |
| 63 | + { |
| 64 | + UnhandledException?.Invoke(sender, new UnhandledExceptionEventArgs(args.Exception, true)); |
| 65 | + }; |
| 66 | +#endif |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments