本文介绍了处理全局异常 Xamarin |机器人 |IOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

如何确定问题及其原因?

解决方案

目的: 我们的目的是获取异常的堆栈跟踪数据,帮助我们确定问题的确切原因是否在 发布模式调试模式.我们将能够了解问题及其原因.我们将这些数据存储在一个 text 文件中,该文件将存储在设备存储中.


解决方案:或者,您可以制作自己的洞察力采集器,如果在测试应用程序时出现问题,它可以为您提供应用洞察力和线索.它将是您的,您可以根据需要进行调整.让我们深入了解全局 try{}catch{}.

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

公共静态类ExceptionFileWriter{#region 属性文件路径静态字符串文件路径{得到{字符串路径 = string.Empty;var _fileName = "Fatal.txt";#if __IOS__字符串文档路径 = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//文档文件夹 C:ddddd字符串 libraryPath = Path.Combine(documentsPath, ..", Library");//库文件夹 C:dddd...librarypath = Path.Combine(libraryPath, _fileName);//c:ddddd...libraryNBCCSeva.db3#别的#if __ANDROID__string dir = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(),异常");如果(目录.存在(目录))返回 Path.Combine(dir, _fileName);path= Path.Combine(Directory.CreateDirectory(dir).FullName, _fileName);#万一#万一返回路径;}}#endregion#region ToLog 异常public static void ToLogUnhandledException(这个异常异常){尝试{var errorMessage = String.Format("Time: {0}
Error: Unhandled Exception
{1}

", DateTime.Now, string.IsNullOrEmpty(exception.StackTrace) ? exception.ToString() : 异常.StackTrace);File.WriteAllText(FilePath, errorMessage);}捕获(异常前){//只是抑制任何错误记录异常}}#endregion}


是时候实现代码了:在应用的Application 文件或Splash Activity 中订阅以下事件.在这种情况下,我使用的是应用程序.

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


[应用程序]公共类 ExceptionHandlingApp :应用程序{#region 构造函数public ExceptionHandlingApp(IntPtr javaReference, JniHandleOwnership transfer):基础(java引用,传输){}#endregion#region OnCreate公共覆盖无效 OnCreate(){base.OnCreate();AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;}#endregion#region 任务计划异常私有静态无效TaskSchedulerOnUnobservedTaskException(对象发送者,UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs){var newExc = new Exception(TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception);newExc.ToLogUnhandledException();}#endregion#region 当前域异常私有静态无效 CurrentDomainOnUnhandledException(对象发送者,UnhandledExceptionEventArgs unhandledExceptionEventArgs){var newExc = new Exception(CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception);newExc.ToLogUnhandledException();}#endregion}

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

干杯!!

We all know that mobile is compact platform where we have to look lots of things while building an application. It could be anything e.g. Memory Performance Resolutions Architecture Implementation etc. We never know when and what causes app crash a big ISSUE while playing with the app, It could happen anytime

And trust me sometime its really hard to find where and what cause an issue in app. I saw many post on forums, tech community and groups which is related to the same issue, where peoples usually asking questions as:

  1. App Crashing at launching.
  2. App Crash at Splash Screen loading.
  3. App Crash while Image showing.
  4. App Crashing while binding data from api.

How to identify issue and where it causes?

解决方案

Purpose: Our purpose here to grab an exception's stack trace data that help us to identify what exactly causes the issue whether in Release Mode or Debug Mode. We will be able to understand the issue and the reason that causes it. We will store this data in a text file that will be store in device storage.


Solution: Alternatively you can make your own insight grabber that will give you you app insight and clue if something went wrong while testing the app. Its will be your, you can tweak like you want. let's dive to try{} and catch{} globally.

Create a Helper Class file that has a method to generate a Text file for exception data.

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}
Error: Unhandled Exception
{1}

", DateTime.Now, string.IsNullOrEmpty(exception.StackTrace) ? exception.ToString() : exception.StackTrace);
            File.WriteAllText(FilePath, errorMessage);
        }
        catch (Exception ex)
        {
            // just suppress any error logging exceptions
        }
    }

    #endregion
}


Time to implement code: Subscribe following events inside your app's Application file or Splash Activity. I'm using Application in this case.

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
}

Cheers!!

这篇关于处理全局异常 Xamarin |机器人 |IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 14:19
查看更多