Introduction
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 the 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
Errors
Errors happen when a runtime exception occurs from an unexpected event. Errors often do not terminate the app and are handled by a try/catch block. When an exception occurs, the App Center records the details of the error, then the state of the app. The device also automatically generates an error log based on the records. These logs contain valuable information about the error which helps us in fixing the error.
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. Go to the following
link. Now, sign in using your preferred account.
Add New app
In this step, give your app a name (Ex: MyApp) and a short description.
Afterward, click "Add a 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 Errors" FontAttributes="Bold" FontSize="Large" TextColor="#CA6F1E" HorizontalTextAlignment="Center" ></Label>
- <Button x:Name="btnError" Text="Make Exception" Clicked="btnException_Clicked" />
- </StackLayout>
- </StackLayout>
- </ContentPage>
Click the "Play" button to try it out.
Add AppCenter Crashes NuGet
In this step, you need to add the AppCenter Crashes NuGet 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 for "Microsoft.AppCenter.Crashes" and add this package. Remember to install it for each project (PCL, Android, iOS, and UWP).
AppCenter.Crashes require platform-specific setup
Xamarin.forms PCL
The following steps are necessary for PCL. In the PCL project's App.xaml.cs, 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 Android.
https://appcenter.ms/
UWP
You need to create another app in the App Center with os option Windows.
https://appcenter.ms/
Error
In this step, write the following code for a simple exception.
MainPage.xaml.cs
- using Microsoft.AppCenter;
- using Microsoft.AppCenter.Crashes;
- using Xamarin.Forms;
-
- namespace XamarinAppCenter
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- }
-
- void btnException_Clicked(object sender, System.EventArgs e)
- {
- try
- {
- int a = Convert.ToInt32("xamarin");
- }
- catch (Exception exception)
- {
- Crashes.TrackError(exception);
- }
- }
-
-
- }
- }
Click the play button to try it out.
Errors
Error Details
I hope you have understood how to collect Error log Using Visual Studio App Center in Xamarin.Forms.
Thanks for reading. Please share comments and feedback.