Log Unity Exceptions with Raygun4Unity Beta

| 5 min. (885 words)

Today we are pleased to announce the beta of Raygun4Unity – first class support for sending error information from Unity games to Raygun. This supports Windows desktop, Windows Phone, Mac, iOS and Android.

Once your game has been released to the world, the last thing you want to hear is that players have been having a bad experience due to crashes and bugs. What’s even worse is trying to work out how to reproduce these bugs and solve them. By integrating Raygun4Unity into your game, you’ll get information about all unhandled exceptions logged straight to your Raygun dashboard. This gives you loads of information that can be used to track down and fix bugs. Additionally, you won’t need to rely on the community to learn about what issues have occurred. All exception occurrences will be displayed in your dashboard giving you a view of the overall health of your game. Information can be filtered and searched, and the charts will plot the improvement of your game as you release patches or new versions.

How to use Raygun4Unity

To get Raygun4Unity, head over to the latest release listed in GitHub and download Raygun4Unity.zip. Extract the contents, and paste the Raygun4Unity folder somewhere in the Assets directory of your Unity game. (When the first version of Raygun4Unity is released, we’ll look at providing it through the Unity assets store).

Now you can use Raygun4Unity to send exception information to Raygun. To do this, simply create an instance of RaygunClient by passing in your application API key into the constructor, then call one of the RaygunClient.Send methods.

(You get an API key when you create a new application in your Raygunaccount. The key is displayed in the setup instructions page, and can also be found in the application settings).

Odds are that you already have a central place in your game where you at least log exception information out to a file. If so, this is a great place to send the exception information to Raygun from. If however you don’t currently catch unhandled exceptions in your game, a common way to do this is to listen to Application.RegisterLogCallback. In the following example, Application.RegisterLogCallback has been set up in a MonoBehaviour that could be run during the initialization process of the game. The handler provides the message and the stack trace that can be sent in one of the RaygunClient.Send methods. Here you can see I’m checking the type of the log message so as to only send exceptions and errors, but you could send other log messages too.

public class Logger : MonoBehaviour
{
  void Start ()
  {
    Application.RegisterLogCallback(HandleException);
  }

  private void HandleException(string message, string stackTrace, LogType type)
  {
    if (type == LogType.Exception || type == LogType.Error)
    {
      RaygunClient client = new RaygunClient("YOUR_APP_API_KEY");
      client.Send(message, stackTrace);
    }
  }
}

Sometimes you may want to send information about handled exceptions – ones that aren’t critical to the game, but you may be interested in how often they occur, along with information about how to solve them. This can be done by sending the Exception object from within a try/catch block.

try
{

}
catch (Exception e)
{
  RaygunClient client = new RaygunClient("YOUR_APP_API_KEY");
  client.Send(e);
}

Overloads of the Send method allow you to send a list of tags or/and a dictionary of custom data. These can be set however you like and can be a huge help to debugging. You could use this to send the values of useful local variables, the state of the current player, or information about the loaded level.

Application Version

Knowing which version of your game each exception came from can help determine if an exception has already been resolved in a more recent version. Exceptions can also be filtered by version on your Raygun dashboard. To use this feature, set the ApplicationVersion property of the RaygunClient instance that you are using to send exception information.

User tracking

All the Raygun providers allow you to optionally specify user information which gives you an idea of the impact of each type of exception. When viewing information about an exception in Raygun, the number of users that were affected by that exception will be displayed. You could use this to prioritize which bugs to invest time into solving first. It’s easy to specify the user information by setting the UserInfo property of the RaygunClient instance. What information you provide is up to you, but it could include a name, email address or some kind of unique identifier, possibly from a database.

Get Raygun4Unity now

Once Raygun4Unity is set up in your game, all unhandled exception information will be logged to your Raygun dashbaord. This information includes as much of the stack trace that’s available, the screen resolution, the loaded level name, information about the platform that the game is running on, the graphics device, the device locale and of course any custom data that you’ve provided.

If you’re working on a Unity game project, we recommend trying out the beta of Raygun4Unity to give you awareness of the health of your game out in the wild. If you have any questions or feedback about it, we’d love to hear from you in the forums.

If you haven’t already, get started for free with a 14 day trial and see how Raygun can improve your Unity game development.