sentry-dotnet: Failed to extract body - NullReferenceException
Package
Sentry.AspNetCore
.NET Flavor
.NET
.NET Version
6.0.201
OS
Windows
SDK Version
3.16.0
Self-Hosted Sentry Version
No response
Steps to Reproduce
We could not reproduce the issue, it happens randomly and seldomly but it makes the server crash
Expected Result
The error should be triggered only once
Actual Result
It seems that NLog logs properly but when sending the log to Sentry it enters in a infinite loop.
2022-06-06 10:25:29.7338|ERROR|App.Controllers.StudyController|Unhandled controller exception: Object reference not set to an instance of an object.|System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_BodyReader()
at Microsoft.AspNetCore.Http.Features.FormFeature.InnerReadFormAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Mvc.ModelBinding.FormValueProviderFactory.AddValueProviderAsync(ValueProviderFactoryContext context)
at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.CreateAsync(ActionContext actionContext, IList`1 factories)
at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.TryCreateAsync(ActionContext actionContext, IList`1 factories)
at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<<CreateBinderDelegate>g__Bind|0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|26_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
2022-06-06 10:25:29.8449|ERROR|Sentry.ISentryClient|Failed to extract body.|System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_BodyReader()
at Microsoft.AspNetCore.Http.Features.FormFeature.InnerReadFormAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Features.FormFeature.ReadForm()
at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Form()
at Sentry.AspNetCore.HttpRequestAdapter.get_Form() in /_/src/Sentry.AspNetCore/HttpRequestAdapter.cs:line 17
at Sentry.Extensibility.FormRequestPayloadExtractor.DoExtractPayLoad(IHttpRequest request) in /_/src/Sentry/Extensibility/FormRequestPayloadExtractor.cs:line 24
at Sentry.Extensibility.BaseRequestPayloadExtractor.ExtractPayload(IHttpRequest request) in /_/src/Sentry/Extensibility/BaseRequestPayloadExtractor.cs:line 33
at Sentry.Extensibility.RequestBodyExtractionDispatcher.ExtractPayload(IHttpRequest request) in /_/src/Sentry/Extensibility/RequestBodyExtractionDispatcher.cs:line 53
at Sentry.AspNetCore.ScopeExtensions.SetBody(Scope scope, HttpContext context, SentryAspNetCoreOptions options) in /_/src/Sentry.AspNetCore/ScopeExtensions.cs:line 163
at Sentry.AspNetCore.ScopeExtensions.Populate(Scope scope, HttpContext context, SentryAspNetCoreOptions options) in /_/src/Sentry.AspNetCore/ScopeExtensions.cs:line 55
2022-06-06 10:25:29.9017|ERROR|Sentry.ISentryClient|Failed to extract body.|System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_BodyReader()
at Microsoft.AspNetCore.Http.Features.FormFeature.InnerReadFormAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Features.FormFeature.ReadForm()
at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Form()
at Sentry.AspNetCore.HttpRequestAdapter.get_Form() in /_/src/Sentry.AspNetCore/HttpRequestAdapter.cs:line 17
at Sentry.Extensibility.FormRequestPayloadExtractor.DoExtractPayLoad(IHttpRequest request) in /_/src/Sentry/Extensibility/FormRequestPayloadExtractor.cs:line 24
at Sentry.Extensibility.BaseRequestPayloadExtractor.ExtractPayload(IHttpRequest request) in /_/src/Sentry/Extensibility/BaseRequestPayloadExtractor.cs:line 33
at Sentry.Extensibility.RequestBodyExtractionDispatcher.ExtractPayload(IHttpRequest request) in /_/src/Sentry/Extensibility/RequestBodyExtractionDispatcher.cs:line 53
at Sentry.AspNetCore.ScopeExtensions.SetBody(Scope scope, HttpContext context, SentryAspNetCoreOptions options) in /_/src/Sentry.AspNetCore/ScopeExtensions.cs:line 163
at Sentry.AspNetCore.ScopeExtensions.Populate(Scope scope, HttpContext context, SentryAspNetCoreOptions options) in /_/src/Sentry.AspNetCore/ScopeExtensions.cs:line 55
2022-06-06 10:25:29.9461|ERROR|Sentry.ISentryClient|Failed to extract body.|System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_BodyReader()
at Microsoft.AspNetCore.Http.Features.FormFeature.InnerReadFormAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Features.FormFeature.ReadForm()
at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Form()
at Sentry.AspNetCore.HttpRequestAdapter.get_Form() in /_/src/Sentry.AspNetCore/HttpRequestAdapter.cs:line 17
at Sentry.Extensibility.FormRequestPayloadExtractor.DoExtractPayLoad(IHttpRequest request) in /_/src/Sentry/Extensibility/FormRequestPayloadExtractor.cs:line 24
at Sentry.Extensibility.BaseRequestPayloadExtractor.ExtractPayload(IHttpRequest request) in /_/src/Sentry/Extensibility/BaseRequestPayloadExtractor.cs:line 33
at Sentry.Extensibility.RequestBodyExtractionDispatcher.ExtractPayload(IHttpRequest request) in /_/src/Sentry/Extensibility/RequestBodyExtractionDispatcher.cs:line 53
at Sentry.AspNetCore.ScopeExtensions.SetBody(Scope scope, HttpContext context, SentryAspNetCoreOptions options) in /_/src/Sentry.AspNetCore/ScopeExtensions.cs:line 163
at Sentry.AspNetCore.ScopeExtensions.Populate(Scope scope, HttpContext context, SentryAspNetCoreOptions options) in /_/src/Sentry.AspNetCore/ScopeExtensions.cs:line 55
2022-06-06 10:25:30.0173|ERROR|Sentry.ISentryClient|Failed to extract body.|System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_BodyReader()
at Microsoft.AspNetCore.Http.Features.FormFeature.InnerReadFormAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Features.FormFeature.ReadForm()
at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Form()
at Sentry.AspNetCore.HttpRequestAdapter.get_Form() in /_/src/Sentry.AspNetCore/HttpRequestAdapter.cs:line 17
at Sentry.Extensibility.FormRequestPayloadExtractor.DoExtractPayLoad(IHttpRequest request) in /_/src/Sentry/Extensibility/FormRequestPayloadExtractor.cs:line 24
at Sentry.Extensibility.BaseRequestPayloadExtractor.ExtractPayload(IHttpRequest request) in /_/src/Sentry/Extensibility/BaseRequestPayloadExtractor.cs:line 33
at Sentry.Extensibility.RequestBodyExtractionDispatcher.ExtractPayload(IHttpRequest request) in /_/src/Sentry/Extensibility/RequestBodyExtractionDispatcher.cs:line 53
at Sentry.AspNetCore.ScopeExtensions.SetBody(Scope scope, HttpContext context, SentryAspNetCoreOptions options) in /_/src/Sentry.AspNetCore/ScopeExtensions.cs:line 163
at Sentry.AspNetCore.ScopeExtensions.Populate(Scope scope, HttpContext context, SentryAspNetCoreOptions options) in /_/src/Sentry.AspNetCore/ScopeExtensions.cs:line 55
2022-06-06 10:25:30.0571|ERROR|Sentry.ISentryClient|Failed to extract body.|System.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 20 (7 by maintainers)
https://github.com/NLog/NLog.Web/pull/754
This was removed outright in NLog.Web.AspNetCore 5.0 due to these issues. I added a PR to fix aspnet-request-posted-body for NLog.Web.AspNetCore 5.1, it requires you to install an ASP.NET Middleware class we added the NLog.Web.AspnetCore to capture the request, that way it is thread safe. I have been using that way to capture both the request and response body at my work for that past 2 years and have had no issues under load. Just remember to use it for requests only less than 30 KB in length. Above that ASP.NET Core HttpRequest.EnableBuffering() writes to TEMP files, according to its documentation. Therefore, our default request length above which we refuse to capture the request body is therefore 30 KB, but you could override that if you want to live dangerously. (I also added about a dozen more layout renderers for asp.net and asp.net core for the upcoming release). We are also adding an example to the wiki for response body capturing middleware. If you need it re-activated before the release I can show you how to add the request middleware class and log the request body to HttpContext.Items, then use one of the existing layout renders that prints an HttpContext.Item out.
Thanks to @bakgerman now NLog.Web.AspNetCore v5.1 re-introduces ${aspnet-request-posted-body} with help from middleware:
It is no longer necessary to explict call
context.Request.EnableBuffering();as it is handled by the NLog-middleware.I see that we have been using
aspnet-request-posted-bodyfor quite some time (many years and many .NET versions). It should have failed but as Sentry enables request buffering in the middleware we didn’t notice any issue with NLog. It makes complete sense: it’s a concurrency problem and only happens to us when we have a lot of traffic. I’ve already deployed a version without this NLog template, let’s see how it performs.Thanks for your tips.