Building and shipping a successful app is a challenge. Monitoring and keeping your app healthy is even more challenging and time-consuming. Once you ship your app into the wild, unexpected errors often occur as real users start engaging with it; staying on top of them is crucial to the success of your app and business. As a developer, you need to have insight into the cause of these issues and how frequently they’re happening. Visual Studio App Center recently shipped a feature that gives you just that awareness. We’re excited to announce the release of the Errors feature for iOS and Android apps built using Xamarin.
In this post, you’ll learn how you can use and make the most of App Center Errors to deal with the errors happening in your Xamarin apps, leading to a better experience for your end users.
Errors: What You Need to Know
Exception handling in C# helps you deal with unexpected or exceptional situations that happen while a program is running, indicating that an error has occurred. To handle these failures, you can use a try/catch block.
Exceptions in C# are defined by type and properties such as the stack trace and message. The caught exception object contains information about the error, such as the state of the call stack and a text description of the error.
The example below shows how to catch and throw an exception of type IndexOutOfRangeException
within your app when the index in an array is out of range:
int GetInt(int[] array, int index)
{
try
{
return array[index];
}
catch(System.IndexOutOfRangeException ex)
{
throw new System.ArgumentOutOfRangeException(
"The parameter index is out of range", ex);
}
}
When an error occurs outside of a try/catch block, it’s said to be an uncaught error. These are crashes and will lead your application to exit. By using a try/catch block, you can enclose your code and handle failures as you need. This bring you the following benefits:
- Improved app reliability and stability.
- Reduced lag times.
- Ensures users can access all app functionality.
To learn more about how and when to use exceptions in C#, take a look at the official documentation.
App Center Service for Errors
Visual Studio App Center Diagnostics is divided into Crashes and Errors for Xamarin apps. The Crashes sections include the uncaught errors, which cause the application to exit and are automatically captured when integrating the App Center SDK. The Errors section in App Center corresponds to the handled errors (known as exceptions in C#). These are reported where defined by the app developer. When running the App Center Crashes SDK module in an application, the service will report the tracked errors during the lifetime of the application. These errors are sent to the server when they occur, provided there is a network connection, or the next time the application is started.
When an app is running, a high amount of error instances are generated, and it can get overwhelming to fix all of them. To make the process more manageable, as well as get quick insights, App Center intelligently groups errors based on the similarity of their stack traces to make it easier and faster for you to diagnose and troubleshoot. By grouping them, you’ll instantly receive information about the most common root cause of your errors, helping you prioritize which errors need attention first. Also, by tracking the status of each group, you can easily manage which errors have already been fixed, which ones you decide are not relevant and can ignore, and which are still open.
The following image shows the error group Overview page in App Center, where the different generated groups are listed, with counts on number of reports and users affected, status, and time.
Fig. 1. Error groups overview in App Center.
On top of the error groups, App Center provides you with information on the most affected devices and OS, as seen below.
Fig. 2. Statistics for a generated error group in App Center.
To get to the root cause of your error and understand why and where it happened in your code, you can easily drill down a detailed stack trace and get information about the device properties, such as model, OS, country, language, etc.
Fig. 3. Error Instance Detail Page in App Center.
For further debugging, you can attach custom properties to these errors, such as “WiFi status”, “File name”, “Category”, and more.
You can find more details on the feature set available in the Errors documentation.
How to Get Started with Errors in App Center
Errors in Xamarin apps are now available in preview in App Center.
To start tracking errors, simply follow a few steps to integrate the App Center Crashes SDK. Learn more in the SDK Documentation.
Once you integrate the Crashes SDK, you can use the TrackError
 method. Here’s an example for a common scenario where you divide a number by 0, which would result in a DivideByZeroException
error.
static void Main()
{
double X = 15, Y = 0;
double output = 0;
try
{
output = X / int.Parse(Y);
}
catch (DivideByZeroException ex)
{
Crashes.TrackError(ex);
}
}
You can also add custom properties to your errors to get more insights into what’s happening. Simply pass a Dictionary of strings key/value pairs to the TrackError
method that we defined earlier. In the example code below, the custom properties will be used to track the filename, location, and the type of issue when the error occurred. Try:
{
using (var text = File.OpenText("saved_game001.txt"))
{
Console.WriteLine("{0}", text.ReadLine());
...
}
}
catch (FileNotFoundException ex)
{
Crashes.TrackError(ex, new Dictionary{
{ "Filename", "saved_game001.txt" },
{ "Where", "Reload game" },
{ "Issue", "Index of available games is corrupted" }
});
}
As you can see, errors can happen at any point when customers are using your app, and it’s important that you find out about and fix them right away. App Center Errors will enable you to track your app and provide you with key information about issues and errors in your app in an organized, visual, and concise way, so you can easily diagnose and fix problems before more of your customers run into them.
App Center Errors is completely free. Sign up today to integrate the App Center Crashes SDK and start tracking errors in your Xamarin apps! If you have any questions or feedback, please reach out to us via the in-portal support system.