Introduction
In this article, you will learn how to collect crash reports using Visual Studio App Center in Xamarin.Forms. Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that the process of reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
Visual Studio App Center
App Center has multiple services that are most commonly used by mobile developers, where multiple services act as a single integrated product. With the use of a single integrated product, you can build, test, distribute, and monitor your mobile apps, and also implement push notifications.
Support Platforms
- Android
- iOS
- React Native
- UWP
- Xamarin
- macOS
- Cordova
App Center Services
- Build
- Diagnostics (Formerly Crashes)
- Test
- Analytics
- Distribute
- Push Notifications
Crashes
Crashes happen when a runtime exception occurs from an unexpected event. Usually, a crash terminates the app. These are the errors not handled by a try/catch block. When a crash occurs, App Center will record the details of the crash, then the state of the app and device automatically generate a crash log based on the records. These logs contain valuable information about the crash which helps us fix the crash.
Prerequisites
- Visual Studio 2017(Windows or Mac)
- Visual Studio App Center Account
Setting up a Xamarin.Forms Project
Start by creating a new Xamarin.Forms project. You’ll learn more by going through the steps yourself. Choose the Xamarin.Forms App Project type under Cross-platform/App in the New Project dialog.
Name your app, select “Use Shared Library” for shared code, and target both - Android and iOS.
You probably want your project and solution to use the same name as your app. Put it in your preferred folder for projects and click Create.
You now have a basic Xamarin.Forms app. Click the "Play" button to try it out.
Create an app in the App Center (iOS)
In this step, create an app in the App Center by going through the following
link.
Now, sign in using your preferred account.
Add New app
In this step, give your app Name (Ex: MyApp) and description.
Afterward, click Add New app.
Now, your App Center app is ready. You can use it now.
Setting up the User Interface
Go to MainPage.Xaml and write the following code.
MainPage.xaml
- <?xml version="1.0" encoding="utf-8"?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamarinAppCenter" x:Class="XamarinAppCenter.MainPage">
- <StackLayout>
- <StackLayout HorizontalOptions="Center" VerticalOptions="Start">
- <Image Margin="0,50,0,0" x:Name="imgBanner" Source="banner.png" ></Image>
- <Image Margin="0,0,0,10" x:Name="imgAppCenter" HeightRequest="200" Source="Appcenter.jpg" ></Image>
- <Label Margin="0,0,0,10" Text="App Center Crash Report" FontAttributes="Bold" FontSize="Large" TextColor="#CA6F1E" HorizontalTextAlignment="Center" ></Label>
- <Button x:Name="btnCrash" Text="Crash Me" Clicked="btnCrash_Clicked" />
-
- </StackLayout>
- </StackLayout>
- </ContentPage>
Click the Play button to try it out.
Add AppCenter Crashes NuGet
In this step, add AppCenter Crashes to your project. You can install Microsoft.AppCenter.Crashes via
NuGet, or you can browse the source code on
GitHub.
Go to Solution Explorer and select your solution. Right-click and select "Manage NuGet Packages for Solution". Search "Microsoft.AppCenter.Crashes" and add Package. Remember to install it for each project (PCL, Android, iO, and UWP).
AppCenter.Crashes requires platform-specific setup
Xamarin.forms PCL
The following steps are necessary for PCL.
In the PCL project's App.xaml.cs that is launched AppCenter.Crashes must be initialized in the OnStart() method.
App.xaml.cs
- using Microsoft.AppCenter;
- using Microsoft.AppCenter.Crashes;
-
- [assembly: XamlCompilation(XamlCompilationOptions.Compile)]
- namespace XamarinAppCenter
- {
- public partial class App : Application
- {
- public App()
- {
- InitializeComponent();
-
- MainPage = new MainPage();
- }
-
- protected override void OnStart()
- {
-
- AppCenter.Start("ios=d0bde07f-****-4e43-****-1fb50b1023fb;" +
- "uwp={Your UWP App secret here};" +
- "android={Your Android App secret here}",
- typeof(Crashes));
- }
-
- protected override void OnSleep()
- {
-
- }
-
- protected override void OnResume()
- {
-
- }
- }
- }
iOS
The following steps are necessary for iOS.
In the iOS project's AppDelegate that is launched AppCenter.Crashes must be initialized in the FinishedLaunching() method.
AppDelegate.cs
- public override bool FinishedLaunching(UIApplication app, NSDictionary options)
- {
-
- global::Xamarin.Forms.Forms.Init();
- LoadApplication(new App());
-
- AppCenter.Start("d0bde07f-****-4e43-****-1fb50b1023fb", typeof(Crashes));
-
- return base.FinishedLaunching(app, options);
- }
Android
You need to create another app in the App Center with OS option as Android.
https://appcenter.ms/
UWP
You need to create another app in the App Center with OS option as Windows.
https://appcenter.ms/
Crash
In this step, write the following code for a simple crash.
MainPage.xaml.cs
- using Xamarin.Forms;
-
- namespace XamarinAppCenter
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- }
-
- void btnCrash_Clicked(object sender, System.EventArgs e)
- {
- throw new NotImplementedException();
- }
- }
- }
Click the Play button to try it out.
Crash Details
Crash Group Details
I hope you have understood how to collect Crash Reports using Visual Studio App Center in Xamarin.Forms.
Thanks for reading. Please share comments and feedback.