处理全局异常Xamarin

处理全局异常Xamarin

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

问题描述

我们都知道移动平台是紧凑的平台,在构建应用程序时我们必须要看很多东西.它可以是任何东西内存 性能 解决方案 体系结构 实施等在使用该应用程序时崩溃了一个大问题,它随时可能发生

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. 启动时应用崩溃.
  2. 启动屏幕加载时应用崩溃.
  3. 显示图片时应用崩溃.
  4. 从api绑定数据时应用崩溃.

如何识别问题及其原因?

推荐答案

目的:我们这里的目的是获取异常的堆栈跟踪数据,以帮助我们确定到底是什么原因导致了该问题,无论是在中>发布模式或调试模式.我们将能够了解问题及其原因.我们会将这些数据存储在一个 text 文件中,该文件将存储在设备存储中.

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.

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

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.

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

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\...\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 中订阅以下事件.在这种情况下,我正在使用应用程序.


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
}

干杯!

结果视频

全文

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

08-20 14:14