Sunday, 4 May 2014

Using BugSense for Windows Phone with Caliburn.Micro

BugSense is a 3rd party service I came across recently being promoted on Nokia's (now Microsoft's) DVLUP site. They provide aggregation and reporting of errors from your apps.

All of the usual platforms are supported, and conveniently they provide NuGet packages to facilitate integrating with Windows Phone 7, 8 and Windows Store apps.

I like using Caliburn Micro (CM) for most of my Windows Phone apps to help with using the MVVM pattern, and one of the requirements for using CM is to strip out the contents of the App.xaml.cs file's constructor, leaving just

public App()
{
    // Standard XAML initialization
    InitializeComponent();
}

The BugSense instructions however assume you're using a regular Windows Phone project that has the original App.xaml.cs contents. The workaround I've settled on is to add the BugSense code to the body of the Configure method in the CM AppBootstrapper class.

First, add the BugSense namespaces:

using BugSense;
using BugSense.Core.Model;

Then initialise BugSense (replacing API_KEY with the key displayed when you click on 'Help me integrate' on the BugSense site:

protected override void Configure()
{
    // Initialize BugSense
    BugSenseHandler.Instance.InitAndStartSession(new ExceptionManager(App.Current), RootFrame, "API_KEY");

You might also wish to add logging of handled exceptions too:

catch (Exception ex)
{
    BugSenseLogResult logResult = BugSenseHandler.Instance.LogException(ex);

    // Examine the ResultState to determine whether it was successful.
    Debug.WriteLine("Result: {0}", logResult.ResultState.ToString());

    Debug.WriteLine(ex);
}

In a future post I hope to describe logging exceptions for background tasks.

No comments: