Getting detailed exception info for unhandled exceptions in Xamarin Android

久未见 提交于 2020-01-06 04:50:11

问题


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

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

How can I drill down into the inner exceptions?


回答1:


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.

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.

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.

Hope this helps.

            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
                    );
            };


来源:https://stackoverflow.com/questions/49528533/getting-detailed-exception-info-for-unhandled-exceptions-in-xamarin-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!