Testing AppCenter Crash Reporting in Xamarin

Wiring up Analytics and Crash Reporting is one of the very first things you should do when building out a Xamarin.Forms app. Microsoft AppCenter offers a free service to collect Diagnostics and Analytics (and much more!). In AppCenter, crashes look something like this: 

You get a stack trace, some analytics on number of crashes, number of affected users, affected devices and OS version. You can set the status of your crash as ‘Open’, ‘Closed’ or ‘Ignored’ and include a comment. One of my favorite features is making a note for a particular bug and being able to review the list quickly with hover capability on those notes…

To get rolling:

1. Sign up for an AppCenter account and create an App for each of your targeted Xamarin.Forms platforms. If you are targeting iOS, Android and UWP, you will need to create an app for each one in AppCenter. If your project requires environments for Dev, QA, Training and Production, and are targeting all three platforms, you will need 12 AppCenter apps!

2. Install the AppCenter nuget packages and add one line of code to your OnStart() method in App.xaml.cs. Refer to the directions in the Overview section of the AppCenter portal. Your ‘App Secret’ codes can be found in that section as well.

AppCenter.Start("ios={Your iOS App secret here};" +
                  "uwp={Your UWP App secret here};" +
                  "android={Your Android App secret here};",
                  typeof(Analytics), typeof(Crashes));

3. In your ViewModel, add the following command. You can use this vanilla implementation or any implementation of the ICommand interface, like RelayCommand from MVVMLight.

//also add:   using Microsoft.AppCenter.Crashes;
public Command CrashCommand { get { return new Command(() => Crashes.GenerateTestCrash()); } }

4. In your View, add the following button:

<Button Text="CRASH!" Command="{Binding CrashCommand}" />

5. Run the app, Tap the new button & Crash!!

If you want to take a look at a more realistic stack trace in AppCenter, change your viewmodel command to divide by zero!

Leave a Comment

Your email address will not be published. Required fields are marked *