Scopes for Unity
Learn what the scope is in the Sentry Unity SDK, and how to attach data to all events or to a single event.
When an event is captured and sent to Sentry, the SDK merges that event with the data stored on the scope, such as tags, contexts, breadcrumbs, and the user.
Unity runs in global mode, with one scope shared by every thread. Data you set on the scope stays there and is added to every event until you change or clear it. PushScope has no effect.
Native crashes include most of this data too. Learn more in Scope Sync.
The most useful operation when working with scopes is the ConfigureScope function. It can be used to reconfigure the current scope.
You can, for instance, add custom tags or inform Sentry about the currently authenticated user.
using Sentry;
using Sentry.Unity;
SentrySdk.ConfigureScope(scope =>
{
scope.SetTag("my-tag", "my value");
scope.User = new SentryUser
{
Id = "42",
Email = "john.doe@example.com"
};
});
using Sentry;
using Sentry.Unity;
SentrySdk.ConfigureScope(scope =>
{
scope.SetTag("my-tag", "my value");
scope.User = new SentryUser
{
Id = "42",
Email = "john.doe@example.com"
};
});
You can also apply this configuration when unsetting a user at logout:
SentrySdk.ConfigureScope(scope =>
{
scope.User = new SentryUser();
});
SentrySdk.ConfigureScope(scope =>
{
scope.User = new SentryUser();
});
SentrySdk.ConfigureScope(
fun scope ->
scope.User <- SentryUser()
)
In the following example we use the scope callback parameter that is available for all capture methods to attach a level and a tag to only one specific error:
// will be tagged with my-tag="my value"
SentrySdk.CaptureException(new Exception("my error"), scope =>
{
scope.SetTag("my-tag", "my value");
scope.Level = SentryLevel.Warning;
});
// will not be tagged with my-tag
SentrySdk.CaptureException(new Exception("my other error"));
// will be tagged with my-tag="my value"
SentrySdk.CaptureException(new Exception("my error"), scope =>
{
scope.SetTag("my-tag", "my value");
scope.Level = SentryLevel.Warning;
});
// will not be tagged with my-tag
SentrySdk.CaptureException(new Exception("my other error"));
open Sentry
// will be tagged with my-tag="my value"
SentrySdk.CaptureException (exn "my error",
fun scope ->
scope.SetTag("my-tag", "my value")
scope.Level <- SentryLevel.Warning
)
// will not be tagged with my-tag
SentrySdk.CaptureException(exn "my other error")
Before the callback is invoked the SDK creates a clone of the current scope, and the changes made will stay isolated within the callback function. This allows you to more easily isolate pieces of context information to specific locations in your code or even call clear to briefly remove all context information.
Important
Any exceptions that occur within the callback function for configuring a local scope will not be caught, and all errors that occur will be silently ignored and not reported.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").