Log Events and Crashes in Xamarin Forms with App Center

While there are a number of third party mobile telemetry and logging solutions on the market, Its no secret that I’m partial to the Microsoft platform.  As such, in this post I’ll show you an easy way to enable and send log messages in your Xamarin Forms apps using App Center

Get Set Up

To get started, you’ll need to install the App Center SDK.  This is the shortened version of the documentation in that link.

There are three main NuGet packages offered.  The main App Center package, an Analytics package and a crashes package.  Install them from the Visual Studio NuGet package manger.  If you prefer the package manager console, you’ll want to use the commands

Install-Package Microsoft.AppCenter.Analytics
Install-Package Microsoft.AppCenter.Crashes

The crashes and analytics packages depend on the main App Center package so it will install automatically with them.   You’ll need to install the packages in each project.  To start logging, you need to opt in to any of the modules you want to use.  In Xamarin Forms, call the AppCenter.Start method in your App.Xaml.cs file.  The call looks as shown below, and takes a string argument of app secrets as well as parameters for each module you want to enable.  App secrets are obtained by creating a separate App Center app for each platform you’re using.  So if you have an Android App and an iOS app, you’ll need to create an App Center Android app as well as an iOS app, and get a secret for each.  The method allows you to pass one to three secrets and enable one or both modules, like so

Replace the curly braces and ‘Your App Secret’ with your actual app secret.

Start Logging

Now that you’re all configured you can start calling the crashes module in your catch blocks, or anywhere you want to report a non optimal situation, and call the events module for any event you want to report.   Calls to the crash and event modules are shown below.  Pass an exception object to the crashes call, and a string argument to the event call.  They each also take a Dictionary<string, string> argument where you can pass up to 20 custom properties and values.   There are other limitations, such as 200 distinct event names, a maximum limit of 256 characters per event name and 125 characters per event property name and event property value.

View your Logs in App Center

Call these and the SDK will send the data up to App Center where you will see it in the portal.  In the portal you get session count and device properties like model and OS version for free, meaning no extra code required.  The portal displays your data in aggregate and also gives you some nice charts.  Here’s a couple of images of the events main page and a detail view of one of the events.

You can see from the images above I’m logging some things that I care about in this sample field technician app. Namely: A user logs in, a user creates a ticket, and so forth. In the second image, you can see the drilled in view and details which include the number of users, and if you scrolled down, values of the custom properties I sent.

With App Center, you can even export this data to Azure Blob Storage or Application Insights to gain further insights. Another cool feature is the ability to see log data in real time as it is sent to App Center. Simply navigate to the Analytics > Log Flow menu item in your App Center app, use your app in a way that invokes anywhere in your code where you have wired up an SDK call and BAM, you’ll see the raw data come in. How cool is that? I can’t wait to see what else they might come out with.