Backtrace allows you to configure error reporting for your Unity games by using the backtrace-unity library.

The backtrace-unity library can be downloaded from here.

Prerequisites

  • Unity environment 2017 +
  • .NET 4.5+ scripting runtime version
  • Mono or IL2CPP scripting backend

Setup and Installation

  1. Download the backtrace-unity zip file. Unzip it and keep the folder in a known location.
  2. Open your Unity project and ensure it is configured to build with .NET 4.5+ runtime and a mono or il2cpp backend.
  3. Copy the unzipped folder to your project's asset folder in the Windows File Explorer. The Unity editor will refresh and the Backtrace Plugin should become available in the editor. 

Integrating into your Project

  1. Under the Assets Menu, there is now a Backtrace -> Configuration option. Choose that option to have a Backtrace Configuration is generated in the Assets folder.  
  2. Next, select an object from the Scene Hierarchy to associate the Backtrace reporting client to. In the example below, we use the _Manager object., Using the Inspector panel, click the Add Component button and search for the Backtrace Client object.
  3. Within the Backtrace Client panel, there is a Backtrace Configuration field. Drag and drop the Backtrace Configuration from the Assets folder to that field. More fields will appear for you to fill in to configure the Backtrace Client and Offline Database options.

Watch this 1 minute silent video to see the Integration and Configuration in action. The first 20 seconds of the video shows the above Integrating steps, and the second part shows details of the below Client and Database Settings.

Backtrace Client and Offline Database Settings

The following is a reference guide to the Backtrace Client fields:

  1. Server Address and Token: These fields are required to submit exceptions from your Unity project to your Backtrace instance. More information about how to retrieve these values for your instance is our docs at What is a submission URL and What is a submission token?  NOTE: the backtrace-unity plugin will attempt to autofill the Server Address field  for you based on your instance name (if you enter killerwhales and leave the field, it will autofill with https://killerwhales.sp.backtrace.io:6098
  2. Reports per minute: Limits the number of reports the client will send per minutes. If set to 0, there is no limit. If set to a higher value and the value is reached, the client will not send any reports until the next minute. Further, the BacktraceClient.Send/BacktraceClient.SendAsync method will return false.
  3. Handle unhandle exceptions: Toggle this on or off to set the library to handle unhandled exceptions that are not captured by try-catch blocks.
  4. Enable Database: When this setting is toggled, the backtrace-unity plugin will configure an offline database that will store reports if they can't be submitted do to being offline or not finding a network. When toggled on, there are a number of Database settings to configure. 
  5. Backtrace Database path: This is the path to directory where the Backtrace database will store reports on your game. NOTE: Backtrace database will remove all existing files on database start
  6. Create database directory toggle: If toggled, the library will create the offline database directory if the provided path doesn't exists,
  7. Auto Send Mode: When toggled on, the database will send automatically reports to Backtrace server based on the Retry Settings below. When toggled off, the developer will need to use the Flush method to attempt to send and clear. Recommend that this is toggled on.
  8. Maximum number of records: This is one of two limits you can impose for controlling the growth of the offline store. This setting is the maximum number of stored reports in database. If value is equal to zero, then limit not exists, When the limit is reached, the database will remove the oldest entries.
  9. Maximum database size: This is the second limit you can impose for controlling the growth of the offline store. This setting is the maximum database size in MB. If value is equal to zero, then size is unlimited, When the limit is reached, the database will remove the oldest entries.
  10. Retry interval: If the database is unable to send its record, this setting specifies how many seconds the library should wait between retries. 
  11. Maximum retries: If the database is unable to send its record, this setting specifies the maximum number of retries before the system gives up.
  12. Retry order: This specifies in which order records are sent to the Backtrace server. 

Using in your C# code

You can further configure your game to submit crashes by making further changes in the C# code for your game.

Configuring specific try-catch blocks

If you setup Backtrace client and Backtrace database configuration you can retrieve database and client instances by using GameObject. When you retrieve client instance you can start sending reports from try/catch block in your game! The BacktraceClient.Send method will send an error report to the Backtrace endpoint specified.

 //Read from manager BacktraceClient instance
var backtraceClient = GameObject.Find("_Manager").GetComponent<BacktraceClient>();

 //Read from manager BacktraceClient instance
var database = GameObject.Find("_Manager").GetComponent<BacktraceDatabase>();


try{
    //throw exception here
}
catch(Exception exception){
    var report = new BacktraceReport(exception);
    backtraceClient.Send(report);
}

BacktraceReport Options

The BacktraceReport class represents a single error report. You can  submit custom attributes using the attributes parameter, or attach files by supplying an array of file paths in the attachmentPaths parameter.

try
{
  //throw exception here
}
catch (Exception exception)
{
    var report = new BacktraceReport(
        exception: exception,
        attributes: new Dictionary<string, object>() { { "key", "value" } },
        attachmentPaths: new List<string>() { @"file_path_1", @"file_path_2" }
    );
    backtraceClient.Send(backtraceReport);
}

Notes:

  • If you setup BacktraceClient with BacktraceDatabase and your application is offline or you pass invalid credentials to Backtrace server, reports will be stored in database directory path.

Attaching custom event handlers

Backtrace Client allows you to attach your custom event handlers. For example, you can trigger actions before the Send method:

 //Add your own handler to client API

backtraceClient.BeforeSend =
    (Model.BacktraceData model) =>
    {
        var data = model;
        //do something with data for example:        
        data.Attributes.Add("eventAtrtibute", "EventAttributeValue");
        if(data.Classifier == null || !data.Classifier.Any())
        {
            data.Attachments.Add("path to attachment");
        }

        return data;
    };

 
Backtrace Client currently supports the following events:

  • BeforeSend
  • OnClientReportLimitReached
  • OnServerResponse
  • OnServerError

Reporting unhandled application exceptions

BacktraceClient supports reporting of unhandled application exceptions not captured by your try-catch blocks. To enable reporting of unhandled exceptions even if you don't set this option in Backtrace configuration window use code below:

backtraceClient.HandleApplicationException();

Flush database

When your application starts, database can send stored offline reports. If you want to do make it manually you can use Flush method that allows you to send report to server and then remove it from hard drive. If Send method fails, database will no longer store data.

backtraceDatabase.Flush();

Clearing database

You can clear all data from database without sending it to server by using Clear method. BacktraceDatabase will remove all files and won't send it to server.

backtraceDatabase.Clear();

Advanced Topics

For more information on advanced features and architecture related details, please see the Readme on github - https://github.com/backtrace-labs/backtrace-unity/blob/dev/README.md 

Investigating an Error in Backtrace

Once errors are being reported to your Backtrace instance, you should see them in your Triage and Web Debugger view. See below for a screenshot of the Triage view with some Unity exceptions reported.

The developer who is debugging the error may find it useful to view more details of Exception. They choose the 'View Latest Trace' action to see more details in the Backtrace Web Debugger. Below we can see a list of all attributes submitted with a report. (Note the yield signs are just an indicator that this value is not indexed in Backtrace). We can also see the call stack and details of the selected frame.

Below we see more details above the Environment Variables from the Web Debugger to further assist with investigation.

Did this answer your question?