Capturing sessions

Bugsnag tracks the number of “sessions” that happen within your application. This allows you to compare stability scores between releases and helps you to understand the quality of your releases.

Sessions are captured and reported by default. This behavior can be disabled using the AutoTrackSessions configuration option.

This documentation is for version 6 of the Bugsnag Unity notifier. If you are using older versions, we recommend upgrading to the latest release using our Upgrade guide. Documentation for the previous release can be found on our legacy pages.

Automatic tracking

Bugsnag will automatically report a session each time the app is launched or enters the foreground after being in the background for at least 30 seconds.

Manual session handling

If you want control over what is deemed a session, you can switch off automatic session tracking with the Auto Track Sessions configuration option, and manage the session lifecycle using StartSession, PauseSession and ResumeSession on the Bugsnag client.

You should call these methods at the appropriate time in your application’s lifecycle when you wish to have an active session. Any errors which occur in your application outside of a session will still be reported to Bugsnag but will not count towards your application’s stability score.

Start Session

Starts a new session to which subsequent handled and unhandled events will be attributed to.

Bugsnag.StartSession();

If there is already an active session, it will be replaced with a new one. Use ResumeSession if you only want to start a session when one doesn’t already exist.

Pause Session

Prevents further events being attributed to the current session until the session is resumed or a new session is started.

Bugsnag.PauseSession();

This can be advantageous if, for example, you do not wish the stability score to include crashes in a background service.

Resume Session

Resumes tracking events against the current session, if it was previously paused. If there is was no previous session, a new session is started. This method returns true if there was a session to resume or false if a new session was created.

if (Bugsnag.ResumeSession()) // ...

Sessions are stored in memory for the lifetime of the application process and are not persisted on disk. Therefore calling this method on app startup would start a new session, rather than continuing any previous session.

Discarding and amending sessions

The data captured in a session can be customized by adding an On Session callback as part of your initial Bugsnag configuration.

var config = BugsnagSettingsObject.LoadConfiguration();
config.AddOnSession(session => {
    var userId = getMyUserIdentifier(); // a custom user resolver
    session.SetUser(userId, null, null);
    return true; // Return false to discard
});
Bugsnag.Start(config);

The return value from the callback determines whether the session will be delivered to Bugsnag and so can be used to discard sessions if required.

Adding callbacks

We recommend adding callbacks through Bugsnag’s initial configuration (AddOnSession) to ensure that it is registered as soon as Bugsnag starts. However, the following methods are provided to allow callbacks to be added and removed whilst the application is running:

Func<ISession, bool> cb = session => { /* ... */ };

Bugsnag.AddOnSession(cb);
// ...
Bugsnag.RemoveOnSession(cb);

The Session class

The following information is available on the Session class, the representation of session information passed to in a session callback.

property type description
App App A subset of the App data contained in error events.
Device Device A subset of the Device data contained in error events.
Id String A unique ID for the session.
StartedAt DateTime The timestamp that the session was started.
User/SetUser User The active user for the session.