【问题标题】:Handling Global Exception Xamarin | Droid | iOS处理全局异常 Xamarin |机器人 | iOS
【发布时间】:2016-01-16 19:12:05
【问题描述】:

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

例如应用启动、加载屏幕、API 调用、绑定数据、加载图片等

相信我,有时很难找到导致应用程序出现问题的位置和原因。我在论坛、技术社区和群组上看到了许多与同一问题相关的帖子,人们通常会提出以下问题:

  1. 应用程序在启动时崩溃。
  2. 加载启动画面时应用程序崩溃。
  3. 显示图像时应用程序崩溃。
  4. 从 api 绑定数据时应用程序崩溃。

如何识别问题及其原因?

【问题讨论】:

    标签: c# xamarin xamarin.ios xamarin.android


    【解决方案1】:

    目的:我们在这里的目的是获取异常的堆栈跟踪数据,以帮助我们确定导致问题的确切原因,无论是在Release Mode 还是Debug Mode。我们将能够了解问题及其原因。我们会将这些数据存储在一个text 文件中,该文件将存储在设备存储中。


    解决方案: 或者,您可以制作自己的洞察力采集器,如果在测试应用程序时出现问题,它会为您提供应用程序洞察力和线索。它会是你的,你可以随心所欲地调整。让我们在全球范围内深入了解try{}catch{}

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

    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\...\library\NBCCSeva.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}\r\nError: Unhandled Exception\r\n{1}\n\n", 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 中订阅以下事件。在这种情况下,我使用的是应用程序。

    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
    }
    

    注意:您可以在设备存储中找到异常记录文件|文件管理器 > 异常文件夹 > fatal.txt

    干杯!!

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 当然可以使用 感谢添加,但我猜它与商业版一起使用。如果我错了,请纠正我。
      • 至少在今天,它对所有 Xamarin 客户都是免费且不受限制的。当 Xamarin.Insights 发布时,有这样一句话关于定价:定价将在预览版结束时公布,但将为 Xamarin 订阅者免费提供一个慷慨的计划。 blog.xamarin.com/monitoring-your-apps-with-xamarin-insights
      • 嗯,很好:) 感谢分享@Wosi。
      • 同样在下一个 Xamarin Studio 版本中,Insights 将集成在 Studio 中,我们不必手动输入此代码,很酷的东西
      • *注意:与 2018 年一样,App Center 取代 Xamarin.Insights 作为 Microsoft 的移动应用分析和崩溃报告服务。
      猜你喜欢
      • 2011-05-19
      • 2021-12-05
      • 2017-01-23
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 2011-09-29
      • 2011-06-17
      相关资源
      最近更新 更多