0

I'm trying to log every exception that occurs in my Xamarin.Forms app. I'm only worrying about Android devices at the moment, so far I've seen two approaches to this.

  1. By subscribing to the following events in Xamarin:
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
  1. By subscribing to the following event in Xamarin.Android:
AndroidEnvironment.UnhandledExceptionRaiser += AndroidEnvironmentOnUnhandledException;

Neither of which have worked for me. Those are pretty much the only two solutions you come across when looking for a solution, and since neither are working for me I assume i'm getting something wrong, but i'm stumped. So far I tried throwing a NotImplementedException, a DivideByZeroException and one other, more complicated exception, involving Data Binding, none of which have been caught.

Any help is greatly appreciated, thanks in advance.

Chris
  • 180
  • 1
  • 14
  • 1
    You need to hook into Android and iOS project , see the detailed solution : https://stackoverflow.com/a/39504448/8187800 – ColeX Oct 19 '21 at 08:40
  • @ColeX-MSFT none of the error apis referenced in that post are platform specific, only the file logging is. All of this functionality can be achieved in the cross platform project – Axemasta Oct 20 '21 at 10:47

1 Answers1

2

There are 3 hooks you can use to monitor crashes in your app. It's not the perfect solution that will catch every single exception thrown in your app, but its enough to catch errors & most big crashes (you can almost never catch native crashes like SIGABRT).

AppDomain

AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

This will catch serious exceptions that crash the app domain. You can use AppCenter to track the

void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    var exception = e.ExceptionObject as Exception;

    // Log somewhere
    Console.WriteLine($"App - OnUnhandledException - An exception occurred: {exception}");

    // Using App Center
    Crashes.TrackError((Exception)e.ExceptionObject);
}

TaskScheduler

TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;

This will catch unhandled exceptions in tasks. If you have an async function that bombs out due to an exception, this will catch it. Normally these exceptions are quite invisible in your applications.

void OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
    foreach (var exception in e.Exception.InnerExceptions)
    {
        // Log somewhere
        Console.WriteLine($"App - OnUnobservedTaskException - An exception occured: {exception.ToString()}");

        // Using App Center
        Crashes.TrackError(exception);
    }
}

Xamarin Forms Logs

Log.Listeners.Add(new DelegateLogListener(OnLogListener));

You can listen to the internal logger in forms to get events such as binding errors etc. These logs are very useful when developing as they can quickly tell you why something isn't working between a view & a view model.

void OnLogListener(string arg1, string arg2)
{
    Debug.WriteLine($"App - OnLogListener - {arg1}: {arg2}");
}

App Center

You should definitely consider adding the App Center Crashes SDK to your app. Any unhandled crashes will be uploaded to App Center when the app next loads. This can be a great way of collecting diagnostics for your app.

A word of warning, some crashes that involve native issues caused by linking / missing references are incredibly nasty to debug and can't be handled in code at all. An example I give is when a .dll references a native .a library but the library isn't correctly referenced. This will immediatly kill the app without the chance for you to handle it.

Axemasta
  • 763
  • 1
  • 9
  • 24