Scopes and Hubs
SDKs will typically automatically manage the scopes for you in the framework integrations. Learn what a scope is and how you can use it to your advantage.
When an event is captured and sent to Sentry, SDKs will merge that event data with extra information from the current scope. SDKs will typically automatically manage the scopes for you in the framework integrations and you don't need to think about them. However, you should know what a scope is and how you can use it for your advantage.
You can think of the hub as the central point that our SDKs use to route an event to Sentry. When you call init()
a hub is created and a client and a blank scope are created on it. That hub is then associated with the current thread and will internally hold a stack of scopes.
The scope will hold useful information that should be sent along with the event. For instance contexts or breadcrumbs are stored on the scope. When a scope is pushed, it inherits all data from the parent scope and when it pops all modifications are reverted.
The default SDK integrations will push and pop scopes intelligently. For instance web framework integrations will create and destroy scopes around your routes or controllers.
As you start using an SDK, a scope and hub are automatically created for you out of the box. It's unlikely that you'll interact with the hub directly unless you're writing an integration or you want to create or destroy scopes. Scopes, on the other hand are more user facing. You can call ConfigureScope
at any point in time to modify data stored on the scope. This is useful for doing things like modifying the context.
如果您好奇线程局部性(thread locality)在作用域数据中是如何工作的,请继续阅读。在 .NET 中,有两种子系统用于管理环境数据(即在应用程序的不同部分可用且不需要通过方法参数显式传递的数据):禁用的全局模式和启用的全局模式。通常,Sentry 会自动为您的应用程序类型选择最合适的模式。但是,可以通过手动设置 SentryOptions.IsGlobalModeEnabled
来覆盖此选择。
如果全局模式被禁用(适用于大多数基于服务器的应用程序),环境作用域数据将存储为 AsyncLocal
并随执行上下文流动。Stephen Toub 关于 ExecutionContext vs SynchronizationContext 的博客文章对此概念进行了更详细的解释。
如果全局模式被启用(适用于桌面应用程序),环境作用域数据将作为 Singleton 存储并全局可用。这意味着您可以一次性设置上下文(例如登录到应用程序的用户),并且它将随所有事件一起发送,无论事件是在 UI 线程还是后台线程上捕获的。
When you call a global function such as CaptureEvent
internally Sentry discovers the current hub and asks it to capture an event. Internally the hub will then merge the event with the topmost scope's data.
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.
SentrySdk.ConfigureScope(scope =>
{
scope.SetTag("my-tag", "my value");
scope.User = new User
{
Id = "42",
Email = "john.doe@example.com"
};
});
Or when an asynchronous call is required:
await SentrySdk.ConfigureScopeAsync(async scope =>
{
scope.User = await _context.Users.FindAsync(id);
});
You can also apply this configuration when unsetting a user at logout:
SentrySdk.ConfigureScope(scope =>
{
scope.User = new SentryUser();
});
To learn what useful information can be associated with scopes see the context documentation.
We also support pushing and configuring a scope within a single call. This is typically called WithScope
, PushScope
or implemented as a function parameter on the capture methods, depending on the SDK. It's very helpful if you only want to send data for one specific event.
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"));
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.