Accessing and ensuring global/shared scope in .NET

I also gather that any logging captured on threads would inherit anything applied on the global scope, such as Scope.User , Scope.Contexts.* , tags, and (I hope) breadcrumbs. Is this correct?

Today Scope data is backed by AsyncLocal so once a new Thread is spawned, the Scope is cloned and data is copied. If this is a console app for example, and you ConfigureScope in main thread, any thread that is spawned from there will hold that data but have its own Scope.

  1. On what thread or in what way can we configure scope such that it WOULD be the global one (assuming my idea of global scope is even a thing, where certain core data would always be set, after being set once, across the whole application)?

If you do that on the main thread, that would be the case.
We have a PR open to make an actual “Global Hub” as it exists on Android and iOS, which makes total sense for console, mobile and desktop apps. Is your use case a Desktop app or else?

Ultimately there are other ways to add data to all events like through EventProcessor so I wonder what type of app to clarify if the Global Hub is the way to go here or we could discuss BeforeSend or other ways of “adding data to all events”.

  1. How can it be accessed from elsewhere (threadwise) and added to, such that the data remains present for all logging? I assume SentrySdk.WithScope would be the way to use it, or some clone of it?

WithScope is short for:


SentrySdk.PushScope()
try {
  SentrySdk.ConfigureScope(s => s. ....);
  ....
} finally {
 SentrySdk.PopScope()
}

The current behavior is that Hub is AsyncLocal and hence “Thread Local” but aware of asynchronous code. So there isn’t really any “Global Hub”, except at the point where the Main runs and you have access to the root Hub, before any async happens or any Tasks are spawned.

Should we just retain the scope instance coming off of ConfigureScope , regardless of whether it’s “global” or not, and then use it with WithScope everywhere we want the info to carry across?

Having to take the reference of a scope through that call back would be a work around that we definitly won’t suggest users. If you’re having to write such code, we either fail on providing your with a proper API or documentation on how to tackle a problem in a better way.

Could you give some example of the use case you have at hand? This will help us decide next steps here given that we anyway need to cater for “Global State” approach needed by Mobile (Xamarin) and Desktop which has come up before.