- Xamarin
- App Center
In my previous tutorial I explained why monitoring your application is critical, now let’s see an example with App Center
.
We know why it’s important to have logs during the production cycle of our application.
Now let’s start using App Center
and create a cloud log service that can be reused across your applications.
First of all in your platforms projects, you need to install Microsoft.AppCenter.Analytics
and Microsoft.AppCenter.Crashes
as well as in the shared code project (.Net Standard project for example).
Then go to App Center
website and create a project for each platform project, you will get a secret key for each project.
Let’s create a new class in the shared project called CloudLogService
:
public class CloudLogService : ICloudLogService
{
public void Initialize(string secretKey)
{
Microsoft.AppCenter.AppCenter.Start(secretKey, typeof(Analytics), typeof(Crashes));
}
public void LogEvent(string name, Dictionary<string, string> properties = null)
{
Analytics.TrackEvent(name, properties);
}
public void LogError(Exception ex, Dictionary<string, string> properties = null)
{
Crashes.TrackError(ex, properties);
}
}
In this service, first implement an Initialize
method to setup App Center
analytics and crashes packages. After that we can create methods to log events and errors, they will be automatically sent to the App Center
servers. All the events, errors or crashes are automatically saved in a database locally on the device and when the user of your application have connectivity, everything is autmatically sent to the server, so nothing is lost.
This CloudLogService
is simple and can be adjusted depending on your needs.
I recommend you to create the associated interface for this class to be able to use dependency injection (you will find a complete and simple example at the end of this tutorial).
Finally, if you have a Xamarin Native
or a Xamarin.Forms
project call the Initialize
method below in a common class of your platform projects like this:
new CloudLogService().Initialize("android=YOUR_ANDROID_SECRET_KEY;ios=YOUR_IOS_SECRET_KEY");
Here is some screenshots of what you can get when you send some events and errors to the App Center
server:
Above you can see the list of crashes and errors depending on the versions of your application. These informations will be really useful to prioritize your bug fixes.
For each bug, you will have the stack trace associated with it, the type of device, the version of the OS. This is important if a bug is just reproduced on one specific device:
If you go to your project settings inside the notification panel you can enable email notifications when a new crash group is created, so you will be advertise immediately if something went wrong in your application.
You can have many informations about the usage of your applications in the analytics dashboard just by calling the Initialize
method in your application:
Finally if you place some events logs in your application, you will be able to see them in this list:
I created a complete and simple example of this code using dependency injection with MvvmLightLibs
to show you how it can be used in a project and how easy to reuse it to improve your application.
You will find full source code in this Github repository.
Happy coding !
You liked this tutorial ? Leave a star in the associated Github repository!