Csharp/C#教程:处理全局exceptionXamarin | 机器人| iOS版分享


处理全局exceptionXamarin | 机器人| iOS版

我们都知道移动设备是紧凑型平台,我们在构建应用程序时必须要看很多东西。 它可能是任何东西,例如Memory Performance Resolutions Architecture Implementation等。我们永远不知道什么时候和什么原因导致应用程序在玩应用程序时崩溃一个大问题,它可能随时发生

例如App Launch,Load Screen,API Call,Binding Data,Loading Images等。

并且相信我有时很难找到应用中出现问题的位置和原因。 我在论坛,技术社区和群体上看到了许多与同一问题相关的post,人们通常在这些post中提问:

  1. 应用程序崩溃发布。
  2. 在Splash Screen加载应用崩溃。
  3. 应用崩溃,而图像显示。
  4. App从api绑定数据时崩溃。

如何识别问题及其原因?

目的:我们的目的是获取exception的堆栈跟踪数据,帮助我们确定在Release ModeDebug Mode究竟是什么原因导致问题。 我们将能够理解问题及其产生的原因。 我们将此数据存储在将存储在设备存储中的text文件中。


解决方案:另外,您可以制作自己的洞察力抓取工具,在测试应用程序时,如果出现问题,将为您提供应用程序洞察力和线索。 它将是你的,你可以调整你想要的。 让我们在全球范围内try{}catch{}

创建一个Helper Class文件,该文件具有为exception数据生成文本文件的方法。

 public static class ExceptionFileWriter { #region Property File Path static string FilePath { get { string path = string.Empty; var _fileName = "Fatal.txt"; #if __IOS__ string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // Documents folder C:ddddd string libraryPath = Path.Combine(documentsPath, "..", "Library"); // Library folder C:dddd...library path = Path.Combine(libraryPath, _fileName); //c:ddddd...libraryNBCCSeva.db3 #else #if __ANDROID__ string dir = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), "Exception"); if (Directory.Exists(dir)) return Path.Combine(dir, _fileName); path= Path.Combine(Directory.CreateDirectory(dir).FullName, _fileName); #endif #endif return path; } } #endregion #region ToLog Exception public static void ToLogUnhandledException(this Exception exception) { try { var errorMessage = String.Format("Time: {0}rnError: Unhandled Exceptionrn{1}nn", DateTime.Now, string.IsNullOrEmpty(exception.StackTrace) ? exception.ToString() : exception.StackTrace); File.WriteAllText(FilePath, errorMessage); } catch (Exception ex) { // just suppress any error logging exceptions } } #endregion } 

实现代码的时间:在应用程序的Application文件或Splash Activity订阅以下事件。 我在这种情况下使用Application。

 AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException; 

 [Application] public class ExceptionHandlingApp : Application { #region Constructor public ExceptionHandlingApp(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { } #endregion #region OnCreate public override void OnCreate() { base.OnCreate(); AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException; } #endregion #region Task Schedular Exception private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs) { var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception); newExc.ToLogUnhandledException(); } #endregion #region Current Domain Exception private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs) { var newExc = new Exception("CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception); newExc.ToLogUnhandledException(); } #endregion } 

注意:您可以在Device Storage中找到例外记录文件 文件管理器>例外文件夹> fatal.txt

干杯!!

结果video

全文

除了自己动手之外,您还可以使用Xamarin.Insights,因为它可以免费用于Xamarin用户,并且已经实现了所有平台。 您可以在线接收使用情况报告,崩溃报告等,而无需用户手动向您发送日志文件。

接收崩溃报告时,您唯一需要做的就是在应用启动时初始化Xamarin.Insights:

上述就是C#学习教程:处理全局exceptionXamarin | 机器人| iOS版分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 Insights.HasPendingCrashReport += (sender, isStartupCrash) => { if (isStartupCrash) { Insights.PurgePendingCrashReports().Wait(); } }; Insights.Initialize("Your API Key"); 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1026835.html

(0)
上一篇 2022年1月9日
下一篇 2022年1月9日

精彩推荐