Getting detailed exception info for unhandled exce

2019-08-17 07:59发布

问题:

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