Microsoft’s App Center (https://appcenter.ms/apps) allows us to do several things, including build and distribution of our application(s), but what I’m interested in for this post, is the capabilities which allow us to monitor some usage information, tracing events and viewing exceptions.
Within App Center you need to create an application and tell App Center what OS and Platform is being used and it will respond by supplying you with information on how to “embed” App Center code into your application.
Once we’ve created an application in App Center we’ll be supplied with a key (currently you need a key per OS application).
Let’s now create a simply test application using Xamarin.Forms, although you can create a standard UWP application if you prefer.
Note: at this time WPF does not seem to be fully supported.
Creating our Xamarin.Forms application
- In Visual Studio, select Mobile App (Xamarin.Forms) project, this will allow us to create a UWP, IOS and Android application (ensure all three platforms are selected).
- Now add the two NuGet packages, Microsoft.AppCenter.Analytics and Microsoft.AppCenter.Crashes to each platform specific project.
Inside the shared project, edit the App.xaml.cs adding the following using clauses
using Microsoft.AppCenter; using Microsoft.AppCenter.Analytics;
and within the OnStart, add the following code
AppCenter.Start( "android=ANDROID_KEY;" + "uwp=UWP_KEY;" + "ios=IOS_KEY", typeof(Analytics), typeof(Crashes));
For each platform you need to create a new application key within App Center. If you are only intending to create a single platform version of your application, for example a UWP application, you can use the following instead
AppCenter.Start(UWP_KEY, typeof(Analytics), typeof(Crashes));
If we now run our application, App Center will start to log each time the application is run along with information regarding the OS being used, but what if we want to start to track certain usage, for example to find out if a feature is actually being used.
Exceptions and crash reporting
Crash/Exception reporting is enabled by default but obviously if you’re running inside the Debugger in Visual Studio, this may intercept such exceptions before they get sent to the App Center, so if your using a mobile device, you’ll need to actually deploy the application to test any exception handling.
However in instances where we want to both handle an exception (probably most cases) and log/track the exception we use code like the following
try { // do something } catch(Exception e) { Crashes.TrackError(e); }
We can also supply a dictionary with additional information to TrackError, for example
try { // do something } catch(Exception e) { Crashes.TrackError(e, new Dictionary<string, string> { { "WithFile", filename } }); }
Tracking events
To track events, simple add the following line (obviously replacing the text with whatever you want to see in App Center when an event occurs).
Analytics.TrackEvent("New Feature Used");
we can also supply more information to the event tracking, by supplying a dictionary to the TrackEvent method, for example
Analytics.TrackEvent("New Feature Used", new Dictionary<string, string> { { "WithFile", filename } });
Turning Analytics on/off
Obviously you might want to have a configuration option which allows a user to opt-in/out of analytics, simply use
Analytics.SetEnabledAsync(false);
Offline behaviour
Obviously all this is great if the application/device in connected to the internet but in situations where the application or device are offline, messages will be stored locally. The FAQ states that up to 300 logs can be stored offline and once a internet connection is restored the messages will be sent to the App Center.