Sentry loses its scope inside the ASP.NET pipeline because it uses AsyncLocal for storage
See original GitHub issueThe official Sentry SDK docs for ASP.NET instructs one to start and finish tracing transactions like this in Global.asax.cs
:
protected void Application_BeginRequest()
{
Context.StartSentryTransaction();
}
protected void Application_EndRequest()
{
Context.FinishSentryTransaction();
}
This approach is buggy, as it will sometimes cause the Sentry scope to be lost inside the (non-Core) ASP.NET pipeline. The underlying problem is that the Sentry SDK stores its scopes in an AsyncLocal
field in SentryScopeManager.cs
. This is not a safe way to store such state in (non-Core) ASP.NET. The only safe method is likely to use HttpContext.Items
. For more information, see:
- https://stackoverflow.com/questions/43391498/asynclocal-value-is-null-after-being-set-from-within-application-beginrequest
- https://devblogs.microsoft.com/dotnet/net-framework-4-7-1-asp-net-and-configuration-features/#asp-net-execution-step-feature
- http://piers7.blogspot.com/2005/11/threadstatic-callcontext-and_02.html
In my use case, I only care about the transaction reference, so I applied the following workaround to Global.asax.cs
, based on the above mentioned blog post my Microsoft and how they themselves work around the issue in Application Insights:
public override void Init()
{
base.Init();
OnExecuteRequestStep((httpContextBase, step) =>
{
if (httpContextBase.CurrentNotification == RequestNotification.ExecuteRequestHandler && !httpContextBase.IsPostNotification)
{
SentrySdk.ConfigureScope(scope =>
{
if (scope.Transaction == null)
{
scope.Transaction = (ITransaction) httpContextBase.Items["__SentryTransaction"];
}
});
}
step();
});
}
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:11 (7 by maintainers)
Top Results From Across the Web
Scopes and Hubs for ASP.NET
The scope will hold useful information that should be sent along with the event. For instance contexts or breadcrumbs are stored on the...
Read more >Can't add Sentry to ASP.NET Core Web API project in .NET 6
It seems that AddSentry is not recognized as an extension method of IServiceCollection . I have searched for similar issues and found some ......
Read more >sitemap-questions-71.xml
https://stackoverflow.com/questions/769/solving-a-linear-equation 2021-09-01 ... .com/questions/3502565/error-when-executing-a-stored-proc-in-asp-net ...
Read more >WooodHead
A fast multi-threaded bulk downloader for Oracle queries written in C++11 style and designed to batch process numerous queries concurrently ...
Read more >sitemap-posts-15.xml
... -10-sites-extensions-you-should-use-if-you-are-a-frontend-developer-1110 ... https://dev.to/split/implement-feature-flags-for-entitlements-in-asp-net- ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Tyrrrz
SentrySdk.GetSpan()
now throws aNullReferenceException
from deep within Sentry when executed outside a HTTP request. It used to returnnull
. Is this intentional?The problem is that
.Current
can returnnull
in the getter here, which is unhandled:https://github.com/getsentry/sentry-dotnet/blob/28ec35d278b2fa15d356af41079ab81a956fe364/src/Sentry.AspNet/Internal/HttpContextScopeStackContainer.cs#L11-L15
Also see this related issue: https://github.com/getsentry/sentry-dotnet/issues/1157
Thanks for the fix! I am off for summer holidays, but I might be able to test this in August.