Example of always sending user info in an ASP.Net Core application?

We’ve recently been migrating from a legacy ASP.Net app to now use .NET Core - and I’m wondering the best way to handle grabbing the logged in/authenticated user and stamp it onto all Sentry logs?

Our current flow is like this: In Global.asax.cs, we define Application_Error - and this is what we use to send exceptions to Sentry. We wrapped Sentry calls into our own “ErrorManager” type class. In here, we have logic that grabs the currently logged in email/username/id from our custom authentication classes. So it’s fairly easy to always stamp the credentials onto all outgoing exceptions. We use this same ErrorManager for manual logging as well…

But now, moving to ASP.Net Core - there’s the concept of calling .UseSentry() in our IHostBuilder stuff. Pretty typical for ASP.Net Core I believe - and this is meant to wire up unhandled exceptions to Sentry, correct? But how do I make sure unhandled exceptions always get the data of the logged in user? I thought about hooking into BeforeSend but that doesn’t seem correct.

Is there an example project somewhere showing how to do this? Surely it’s a common issue?

Or should we do something like we’ve done previously - and instead of wiring up Sentry directly to handle unhandled exceptions, we should make our own exception catcher, that in turn calls Sentry?

Any guidance is appreciated!