Xamarin Application Logging: Why, How, and What?

Denys Fiediaiev
5 min readApr 19, 2020

When the app is published, there is always a chance that unexpected issues will happen. In most cases, users will not be able to help, as they don’t know what details are important for developers and cannot explain what exactly happened with the app.

Moreover, users rarely tell developers that something went wrong while using the app. At best, the only information we get is a message that the user was working in the app, and it just closed, losing the user’s data. Today’s apps are complex, and it is difficult to answer, what led to incorrect app behavior.

So how can developers get the necessary information to solve these problems?

They need to know how the user interacted with the app at the moment of failure. To achieve this, they log critical events in a developer-friendly way. Application logging can also help to improve the whole user experience.

Keep in mind that collecting user data may raise some legal issues. It is better to consult with a lawyer.

Today there are application insights logging solutions for various platforms and technologies. App Center from Microsoft is one such tool. It is available for different platforms, including Xamarin and Windows. If you have not yet used it, this is the time to give App Center a try.

Before designing the application logging infrastructure for Xamarin apps, developers need to decide what exactly to log and how to analyze the collected data. This data could be divided into three groups:

Analytical data

Analytical data is business-related information that allows developers to know how users act inside the app so developers can decide the way to improve app features.

For example, if there are several ways to perform the same action in the app, analytics will help to figure out which way users prefer. It is also useful to track the following metrics:

  • Active users: the number of active users indicates whether the application’s audience is growing.
  • OS distribution: developers can think about increasing the minimum OS version to prevent spending resources on supporting old or rarely used OS versions.
  • System language distribution: if the app does not have a specific localization, but the number of users with this language is growing, maybe it is worth adding this language?

App Center has AppCenter.Analytics component to track analytical data. It automatically takes care of the metrics described above, but custom metrics need to be sent manually.

The usage of this component is quite simple: the client specifies the name of the event and the necessary parameters. This sample logs the screen opening with an event called Open Settings and enriches it with information about the screen name and user status: anonymous or authorized.

Using AppCenter.Analytics package to track analytics data

Later, the user’s behavior could be analyzed to find out which screens users visit most often:

Results of sample events tracking.

Failures data

Even if the development team has QA support, real users can break the application in a way that the team does not even guess. This fact makes crash reporting a must-have feature for any production app.

Two types of failures can arise: errors handled by try-catch blocks and unhandled crashes that caused the OS to stop the program execution.

It is hard to say if unhandled crashes are worse than handled errors. Sometimes developers are trying to “survive” at any cost, but it may be better to allow the app to crash than to corrupt the data or the user flow.

In general, crash reporting allows developers to understand the quality of the system and to detect whether or not the application is becoming more stable with the new version.

If there are a smaller amount of crashes with a new version, a team has done an excellent job and is going in the right direction.

Otherwise, it is worth thinking about improving the quality of a codebase and reducing the technical debt. Focusing on manual and automatic testing could help to catch these errors earlier than do users.

To achieve error sending in App Center, the AppCenter.Crashes component could be used. It sends unhandled crashes automatically on the next application launch, so only error reporting needs to be implemented:

Using AppCenter.Crashes package to track failures data.

The caught Exception is mandatory, and other properties are optional. Additionally, an error file could be attached. In this case, it is just a text message.

Result of sample error report.

Tracing data

Some might say that developers should not put these debugging logs into production app versions, but how will they get an understanding of what the user does from the technical perspective? During debugging, it is possible to use some console output, but what to do in the real world?

Since every small logged detail could be useful when looking for the source of an error, the solution would be to write the full application output directly to the file. Developers can send these log files instantly with an error report when using App Center, or provide a user interface that allows users to send these files manually.

The particular implementation depends on the used data organization and storage approach.

Logging of analytical, failure, and tracing data is a significant step forward in improving product quality. It allows developers to get useful feedback from users without interrupting them.

Are you ready to build perfect infrastructure for your project?

So, check out my new post which helps you to do it the right way!

This article describes how to build a convenient application logging infrastructure for a Xamarin application using the MvvmСross framework. So stay tuned!

--

--

Denys Fiediaiev

Xamarin | iOS | Android Developer with 8 years of experience. All things actionable tips, real-life examples and coding guides to help you grow professionally.