1. 程式人生 > 實用技巧 >Unhandled exception handling in iOS and Android with Xamarin.

Unhandled exception handling in iOS and Android with Xamarin.

Unhandled exceptions are hard to catch and log, and you must do it to be able to handle the errors in your app. One approach is to use Xamarin.Insights but you always want to be able to just log into a file and then access that file locally.

The code below is what we use right now and it has helped us a lot, especially the unobserved task exceptions that are just silent, i.e. they do not crash the app, but you still want to handle the exceptions.

  1 // In MainActivity
  2 protected override void OnCreate(Bundle bundle)
  3 {
  4     base.OnCreate(bundle);  
  5      
  6     AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
  7     TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;  
8 9 Xamarin.Forms.Forms.Init(this, bundle); 10 DisplayCrashReport(); 11 12 var app = new App(); 13 LoadApplication(app); 14 } 15 16 ‪#‎region‬ Error handling 17 private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
18 { 19 var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception); 20 LogUnhandledException(newExc); 21 } 22 23 private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs) 24 { 25 var newExc = new Exception("CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception); 26 LogUnhandledException(newExc); 27 } 28 29 internal static void LogUnhandledException(Exception exception) 30 { 31 try 32 { 33 const string errorFileName = "Fatal.log"; 34 var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // iOS: Environment.SpecialFolder.Resources 35 var errorFilePath = Path.Combine(libraryPath, errorFileName); 36 var errorMessage = String.Format("Time: {0}\r\nError: Unhandled Exception\r\n{1}", 37 DateTime.Now, exception.ToString()); 38 File.WriteAllText(errorFilePath, errorMessage); 39 40 // Log to Android Device Logging. 41 Android.Util.Log.Error("Crash Report", errorMessage); 42 } 43 catch 44 { 45 // just suppress any error logging exceptions 46 } 47 } 48 49 /// <summary> 50 // If there is an unhandled exception, the exception information is diplayed 51 // on screen the next time the app is started (only in debug configuration) 52 /// </summary> 53 [Conditional("DEBUG")] 54 private void DisplayCrashReport() 55 { 56 const string errorFilename = "Fatal.log"; 57 var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 58 var errorFilePath = Path.Combine(libraryPath, errorFilename); 59 60 if (!File.Exists(errorFilePath)) 61 { 62 return; 63 } 64 65 var errorText = File.ReadAllText(errorFilePath); 66 new AlertDialog.Builder(this) 67 .SetPositiveButton("Clear", (sender, args) => 68 { 69 File.Delete(errorFilePath); 70 }) 71 .SetNegativeButton("Close", (sender, args) => 72 { 73 // User pressed Close. 74 }) 75 .SetMessage(errorText) 76 .SetTitle("Crash Report") 77 .Show(); 78 } 79 80 ‪#‎endregion‬ 81 82 //iOS: Different than Android. Must be in FinishedLaunching, not in Main. 83 // In AppDelegate 84 public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary options) 85 { 86 AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; 87 TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException; 88 ... 89 } 90 91 /// <summary> 92 // If there is an unhandled exception, the exception information is diplayed 93 // on screen the next time the app is started (only in debug configuration) 94 /// </summary> 95 [Conditional("DEBUG")] 96 private static void DisplayCrashReport() 97 { 98 const string errorFilename = "Fatal.log"; 99 var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Resources); 100 var errorFilePath = Path.Combine(libraryPath, errorFilename); 101 102 if (!File.Exists(errorFilePath)) 103 { 104 return; 105 } 106 107 var errorText = File.ReadAllText(errorFilePath); 108 var alertView = new UIAlertView("Crash Report", errorText, null, "Close", "Clear") { UserInteractionEnabled = true }; 109 alertView.Clicked += (sender, args) => 110 { 111 if (args.ButtonIndex != 0) 112 { 113 File.Delete(errorFilePath); 114 } 115 }; 116 alertView.Show(); 117 }