How to use Raygun with Xamarin Forms

Jason FauchelleProvider Updates2 Comments

This blog post is a simple tutorial to set up Raygun exception reporting in a Xamarin.Forms project. For those that don’t know, Xamarin.Forms allows you to write applications for iOS, Android and Windows Phone using C# .Net with common UI and logic that can be shared in all three platforms. Raygun is an exception reporting service that supports a multitude of different languages and frameworks including the full Xamarin stack.

By using Raygun in a Xamarin.Forms project, you and your team will be notified of exceptions occurring in your published applications. Exception reports include the stack trace, information about the device and any custom data you’ve sent to help you swiftly resolve bugs. Raygun provides valuable insight during development/debugging, beta testing and published applications.

Step 1 – Creating the project

For this tutorial, I am using Visual Studio 2012 on a Windows 7 machine. The other option is to use Xamarin Studio on either a Windows machine or a Mac. In Visual Studio, create a new project and look for “Mobile Apps” under the “Visual C#” category. In the center of the dialog, select “Blank App (Xamarin.Forms Portable)”. Then enter a name for the solution, and hit OK. This will create a new solution containing a few projects – A portable class library to contain common code, and separate projects for iOS and Android.

Xamarin Forms Project

Step 2 – Installation

Next we need to install Raygun4Net. There are two easy options for doing this – via NuGet, or the Xamarin Component Store.

NuGet

To use NuGet, right click the solution and select “Manage NuGet Packages…”. Search for Raygun4Net and then click the install button. This will bring up a small dialog box asking you to pick which projects to install Raygun4Net into. You want to install Raygun4Net into both the iOS and Android projects – these should both be selected by default, so click OK.

Install Raygun4Net with NuGet

Xamarin Component Store

On the other hand, to get Raygun4Net from the Xamarin Component Store, right click on the “Components” folder within the iOS project and select “Edit Components…”. This will open a new tab in Visual Studio where you can manage Xamarin Components. In the top right corner of the tab, click the “Get More Components” button to view the Xamarin Component Store dialog. Search for Raygun4Net, click the search result and then hit the green “Add to App” button. Then repeat this for the Android project.

Raygun4Net Xamarin Component

Add Xamarin Component

Step 3 – Attach the RaygunClient

With Raygun4Net installed, we now just need a single line of code in both the iOS and Android apps to set things up.

iOS – Call RaygunClient.Attach at the start of the Main method within the Main.cs file.

Android – Call RaygunClient.Attach near the start of the OnCreate method within the MainActivity.cs file.

Step 4 – Get your API key

Now all you need is an API key to tell the Raygun service where to log your exception reports. Sign up or log into your Raygun account, then create or navigate to an application where you want to send exception reports. If it is a new application with no exception reports so far, then you’ll see the API key displayed near the top of the page. If the application does already have exception reports, you can find the API key by clicking “Application settings” in the side-bar menu.

Now, replace “YOUR_APP_API_KEY” in the iOS and Android code from the previous step with the real API key. You can either use the same API key for both the iOS and Android projects, or you could use a different API key so that exceptions get logged to different applications in your Raygun account – just do whatever works best for you and your team.

Step 5 – Send a test exception

To make sure everything has been wired up correctly, lets cause the application to crash and see that the exception comes up in your Raygun dashboard. To do this, I’ve replaced the default code of the GetMainPage method within App.cs of the shared PCL project. This displays a button which throws an exception when tapped. As this is in the shared project, it will be used by both the iOS and Android applications, so run them up on either a device or emulator to see that they send the exception report to your Raygun account.

You can download the Visual Studio solution for this tutorial from here. To use this, make sure to replace “YOUR_APP_API_KEY” as described in step 4 above.

Want to get awareness of exceptions occurring in your published Xamarin Forms applications? Sign up for Raygun – 30 day free trial – no credit card required.

Next level software intelligence across your entire stack.

2 Comments on “How to use Raygun with Xamarin Forms”

  1. Guillermo Herrera

    Hello

    I’m trying to install nugget package into my Xamarin.Forms PCL project.
    I got this error:

    Could not install package ‘Mindscape.Raygun4Net 5.5.2’. You are trying to install this package into a project that targets ‘.NETPortable,Version=v4.5,Profile=Profile259’, but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

    1. Jason Fauchelle

      Hi Guillermo Herrera,

      Unfortunately Raygun4Net can not currently be installed into a PCL project. I do not know when we will provide support for this. In the context of a Xamarin Forms application, Raygun4Net can only be installed into the iOS and Android specific projects.

      -Jason Fauchelle

Leave a Reply

Your email address will not be published. Required fields are marked *