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