获取 Xamarin Android 中未处理异常的详细异常信息 [英] Getting detailed exception info for unhandled exceptions in Xamarin Android

查看:35
本文介绍了获取 Xamarin Android 中未处理异常的详细异常信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 Xamarin Android 中遇到未处理的异常,就像在使用 Visual Studio 调试时一样,我会收到以下弹出窗口:

If I get an unhandled exception in Xamarin Android, like always when debugging with Visual Studio, I get the following popup:

如果我点击复制详细信息,我会得到

If I click Copy Details I get

未处理的异常:System.AggregateException:发生一个或多个错误.发生

Unhandled Exception: System.AggregateException: One or more errors occurred. occurred

如何深入了解内部异常?

How can I drill down into the inner exceptions?

推荐答案

这是我们的一个应用程序的片段,我们只是针对当前域的 UnhandledException 事件创建一个委托,我们在应用程序的MainApplication"类中执行此操作(有些人在没有实现生命周期回调插件的情况下使用MainActivity").

Here's a snippet from one of our applications, we simply create a delegate against the UnhandledException event for the current domain, we do this within our applications 'MainApplication' class (Some people use 'MainActivity' instead if they have not implemented the lifecycle call back plugin).

基本上,无论您是应用程序的主要起点,您都希望在哪个类中执行此操作.

Basically you want to do this in whichever class is you're applications primary starting point.

作为参考,'ApplicationLog' 只是我创建的一个类,用于将文件目录写入日志文件,您可以在该类中做任何您想做的事情,这里的关键是 'e' 将包含您的堆栈跟踪信息.

For reference 'ApplicationLog' is just a class I created to write to a file directory into a log file, you can do whatever you want inside this class, the key here is that 'e' will contain your stack trace information.

关于您是个例外,值得注意的是,AggregateException 最常由任务中的代码、并行 ForEach 语句甚至 SQL 命令等引发.

Regards you're exception it is worth noting that an AggregateException is most commonly thrown by code within a Task, or Parallel ForEach statements, or even SQL commands etc.

希望这会有所帮助.

            AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) =>
            {
                // Record the error in our application logs
                ApplicationLog.AppendFile(
                    DateTime.Now.ToString() + "  :  " + "[CRITICAL GLOBALLY HANDLED EXCEPTION] - Critical exception has been hit! - Message: " + e.ExceptionObject +
                        System.Environment.NewLine + System.Environment.NewLine +
                            "========= Critcal Error has been hit, application closed =========" +
                                System.Environment.NewLine + System.Environment.NewLine
                    );
            };

这篇关于获取 Xamarin Android 中未处理异常的详细异常信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆