New .NET user - Scope - SetTag problem

Hi

I am trying to use the .NET SDK. It seems OK, but my Scope seems to be reset somehow.
I have a WPF application, I init SentrySDK in the APP constructor and set some Tags with SetTag. They get set (debug).

Later in the code I also use SetTag, but the original Tags are now missing.
It is the same thread according to VS2019.

When an exception happens, the Tags from the initial use of SetTag gets reported but not the later ones.

I never use PushScope, but nevertheless it seems the scope gets pushed or something.
Ideas / help would be appreciated.

Where do you currently initialize Sentry?

Could you take a look at the docs, it mentions where to init: https://docs.sentry.io/platforms/dotnet/guides/wpf/

It’s worth noting we use AsyncLocal so if you spawn a background thread and set values there, those values are not globally visible. For that we’ll need a global state (which has come up before as a request, and we use that in mobile apps so likely something we should get done sooner rather than later to better support Desktop and Mobile in .NET)

Hi Bruno

I have copied exactly the code from the link PLUS

I initialize the SentrySDK in the APP constructor

Like this:

public App() {

sdksentry = SentrySdk.Init(“https://82d9e186d24f4a4d80c75e5f95b0a275@o484184.ingest.sentry.io/5537058”);

DispatcherUnhandledException += App_DispatcherUnhandledException;

SentrySdk.ConfigureScope(scope => { scope.SetTag(“KBAFILE”, “regnskab”); });

}

void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) {

SentrySdk.CaptureException(e.Exception);

// If you want to avoid the application from crashing:

e.Handled = true;

}

And this works fine.

It is when I call ConfigureSCope later, it is not the same scope or something?

image001.png

Aha, now I understand
I did not understand AsyncLocal - I expected ThreadLocal. But if the Scope is automatically AsyncLocal, that explains it.

So - how do I get to the topScope…
I was ready to put this in production :slight_smile:

I have somewhat solved this.
I save the scope in a public static variable and use that throughout.
I seems to work. See the bold statement below.

    public App() {
        sdksentry = SentrySdk.Init("https://82d9e186d24f4a4d80c75e5f95b0a275@o484184.ingest.sentry.io/5537058");
        DispatcherUnhandledException += App_DispatcherUnhandledException;
        SentrySdk.ConfigureScope(scope => { scope.SetTag("USER", ""); **GlobalScope = scope**; });
    }

For a value set in the constructor of the app, that value will be available everywhere since that’s the root Hub/Scope that gets cloned (indirectly, through the AsyncLocal) to all other async flows.

But with your work around you now have a hold of the scope so you can mutate back and have global side effect.

There was an attempt from a contributor to address is (He uses Xamarin and also is affected by this) but we decided to take another approach.

We have a tracking issue for this: https://github.com/getsentry/sentry-dotnet/issues/628 and we’re prioritizing this now so it should come out on 3.0.0 (with User Feedback, Attachments, Performance monitoring API, nullability annotations, .NET 5 and other goodies)
You can subscribe to the issue to get updates, or subscribe to the repo for notifications of new releases.