Xamarin.Forms - Error Logs In Visual Studio App Center

Introduction

 
Xamarin.Forms - Errors in Visual Studio App Center 
 
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

 
Xamarin.Forms - Errors in 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
  1. Android
  2. iOS
  3. React Native
  4. UWP
  5. Xamarin
  6. macOS
  7. Cordova
Xamarin.Forms - Errors in Visual Studio App Center 

App Center Services

  • Build
  • Diagnostics (Formerly Crashes)
  • Test
  • Analytics
  • Distribute
  • Push Notifications
Xamarin.Forms - Errors in Visual Studio App Center
 
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.
 
Xamarin.Forms - Errors in Visual Studio App Center 
 
Name your app, select “Use Shared Library” for shared code, and target both - Android and iOS.
 
Xamarin.Forms - Errors in Visual Studio App Center 
 
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.
 
Xamarin.Forms - Errors in Visual Studio App Center 
 
You now have a basic Xamarin.Forms app. Click the "Play" button to try it out.
 
Xamarin.Forms - Errors in Visual Studio App Center 

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.
 
Xamarin.Forms - Errors in Visual Studio App Center 
 
Add New app
 
In this step, give your app a name (Ex: MyApp) and a short description.
 
OS iOS
Platform Xamarin
 
Afterward, click "Add a new app".
 
Xamarin.Forms - Errors in Visual Studio App Center 
 
Now, your App Center app is ready. You can use it now.
 
Xamarin.Forms - Errors in Visual Studio App Center 

Setting up the User Interface

Go to MainPage.Xaml and write the following code.
 
MainPage.xaml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <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">  
  3.     <StackLayout>    
  4.         <StackLayout HorizontalOptions="Center" VerticalOptions="Start">    
  5.          <Image Margin="0,50,0,0" x:Name="imgBanner" Source="banner.png" ></Image>    
  6.          <Image Margin="0,0,0,10" x:Name="imgAppCenter" HeightRequest="200" Source="Appcenter.jpg" ></Image>    
  7.          <Label Margin="0,0,0,10" Text="App Center Errors" FontAttributes="Bold" FontSize="Large" TextColor="#CA6F1E" HorizontalTextAlignment="Center" ></Label>    
  8.          <Button x:Name="btnError" Text="Make Exception" Clicked="btnException_Clicked" />    
  9.         </StackLayout>    
  10.     </StackLayout>    
  11. </ContentPage>  
Click the "Play" button to try it out.
 
Xamarin.Forms - Errors in Visual Studio App Center 

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).
 
Xamarin.Forms - Errors in Visual Studio App Center 

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
  1. using Microsoft.AppCenter;  
  2. using Microsoft.AppCenter.Crashes;  
  3.   
  4. [assembly: XamlCompilation(XamlCompilationOptions.Compile)]  
  5. namespace XamarinAppCenter  
  6. {  
  7.     public partial class App : Application  
  8.     {  
  9.         public App()  
  10.         {  
  11.             InitializeComponent();  
  12.   
  13.             MainPage = new MainPage();  
  14.         }  
  15.   
  16.         protected override void OnStart()  
  17.         {  
  18.             // Handle when your app starts  
  19.             AppCenter.Start("ios=d0bde07f-****-4e43-****-1fb50b1023fb;" +  
  20.                   "uwp={Your UWP App secret here};" +  
  21.                   "android={Your Android App secret here}",  
  22.                             typeof(Crashes));  
  23.         }  
  24.   
  25.         protected override void OnSleep()  
  26.         {  
  27.             // Handle when your app sleeps  
  28.         }  
  29.   
  30.         protected override void OnResume()  
  31.         {  
  32.             // Handle when your app resumes  
  33.         }  
  34.     }  
  35. }  
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
  1. public override bool FinishedLaunching(UIApplication app, NSDictionary options)  
  2.         {  
  3.   
  4.             global::Xamarin.Forms.Forms.Init();  
  5.             LoadApplication(new App());  
  6.   
  7.             AppCenter.Start("d0bde07f-****-4e43-****-1fb50b1023fb",  typeof(Crashes));  
  8.   
  9.             return base.FinishedLaunching(app, options);  
  10.         }  
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
  1. using Microsoft.AppCenter;  
  2. using Microsoft.AppCenter.Crashes;  
  3. using Xamarin.Forms;  
  4.   
  5. namespace XamarinAppCenter  
  6. {  
  7.     public partial class MainPage : ContentPage  
  8.     {  
  9.         public MainPage()  
  10.         {  
  11.             InitializeComponent();  
  12.         }  
  13.   
  14.         void btnException_Clicked(object sender, System.EventArgs e)  
  15.         {  
  16.             try  
  17.             {  
  18.                 int a = Convert.ToInt32("xamarin");  
  19.             }  
  20.             catch (Exception exception)  
  21.             {  
  22.                 Crashes.TrackError(exception);  
  23.             }  
  24.         }  
  25.   
  26.   
  27.     }  
  28. }  
Click the play button to try it out.
 
Xamarin.Forms - Errors in Visual Studio App Center 
 
Errors
 
Xamarin.Forms - Errors in Visual Studio App Center 
 
Error Details
 
Xamarin.Forms - Errors in Visual Studio App Center 
 
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.


Similar Articles