EF Core trace stacktraces don't extend into the application
See original GitHub issueDescribe the bug There is something wrong with how stacktraces in awaited async requests get unwound for EF Core in ASP.NET Core. The result is that only system internals are part of the resulting stacktrace, but not the actual method calling the EF Core function.
Example stacktrace as follows:
Elastic.Apm.Model.Span in End
Elastic.Apm.Model.DbSpanCommon in EndSpan
Elastic.Apm.EntityFrameworkCore.EfCoreDiagnosticListener in OnNext
System.Diagnostics.DiagnosticListener in Write
Microsoft.EntityFrameworkCore.Diagnostics.RelationalLoggerExtensions in BroadcastCommandExecuted
Microsoft.EntityFrameworkCore.Diagnostics.RelationalLoggerExtensions in CommandReaderExecutedAsync
Microsoft.EntityFrameworkCore.Storage.RelationalCommand+<ExecuteReaderAsync>d__17 in ExecuteReaderAsync
System.Threading.ExecutionContext in RunInternal
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1 in MoveNext
System.Threading.Tasks.AwaitTaskContinuation in RunOrScheduleAction
System.Threading.Tasks.Task in RunContinuations
System.Threading.Tasks.Task`1 in TrySetResult
MySqlConnector.Core.CommandExecutor+<ExecuteReaderAsync>d__0 in ExecuteReaderAsync at line 76
System.Threading.ExecutionContext in RunInternal
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1 in MoveNext
System.Threading.Tasks.AwaitTaskContinuation in RunOrScheduleAction
System.Threading.Tasks.Task in RunContinuations
System.Threading.Tasks.Task`1 in TrySetResult
MySql.Data.MySqlClient.MySqlDataReader+<CreateAsync>d__95 in CreateAsync at line 409
System.Threading.ExecutionContext in RunInternal
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1 in MoveNext
System.Threading.Tasks.AwaitTaskContinuation in RunOrScheduleAction
System.Threading.Tasks.Task in RunContinuations
System.Threading.Tasks.Task`1 in TrySetResult
MySqlConnector.Core.ResultSet+<ReadResultSetHeaderAsync>d__2 in ReadResultSetHeaderAsync at line 155
System.Threading.ExecutionContext in RunInternal
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1 in MoveNext
System.Threading.Tasks.AwaitTaskContinuation in RunOrScheduleAction
System.Threading.Tasks.Task in RunContinuations
System.Threading.Tasks.Task`1 in TrySetResult
MySqlConnector.Core.ServerSession+<ReceiveReplyAsyncAwaited>d__78 in ReceiveReplyAsyncAwaited at line 775
System.Threading.ExecutionContext in RunInternal
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1 in MoveNext
System.Threading.Tasks.AwaitTaskContinuation in RunOrScheduleAction
System.Threading.Tasks.Task in RunContinuations
System.Threading.Tasks.Task`1 in TrySetResult
MySqlConnector.Protocol.Serialization.ProtocolUtility+<<DoReadPayloadAsync>g__AddContinuation|5_0>d in <DoReadPayloadAsync>g__AddContinuation|5_0 at line 484
System.Threading.ExecutionContext in RunInternal
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1 in MoveNext
System.Threading.Tasks.AwaitTaskContinuation in RunOrScheduleAction
System.Threading.Tasks.Task in RunContinuations
System.Threading.Tasks.Task`1 in TrySetResult
MySqlConnector.Protocol.Serialization.ProtocolUtility+<<ReadPacketAsync>g__AddContinuation|1_0>d in <ReadPacketAsync>g__AddContinuation|1_0 at line 413
System.Threading.ExecutionContext in RunInternal
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1 in MoveNext
System.Threading.Tasks.AwaitTaskContinuation in RunOrScheduleAction
System.Threading.Tasks.Task in RunContinuations
System.Threading.Tasks.Task`1 in TrySetResult
MySqlConnector.Protocol.Serialization.BufferedByteReader+<ReadBytesAsync>d__2 in ReadBytesAsync at line 55
System.Threading.ExecutionContext in RunInternal
It also seems that the stacktraces are fairly random; sometimes they are there and sometimes not, but I assume this is intentional to reduce the impact of tracing.
To Reproduce Steps to reproduce the behavior:
- Configure Elastic APM for ASP.NET Core and EF Core
- Execute some action with EF Core queries in it
- Check the transaction in ElasticSearch APM
Expected behavior Either collect the stacktrace including the application code, or don’t do stacktraces at all for EF Core; they aren’t exactly cheap.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
How to fix EF Core database errors after extending ...
I've built a web app in ASP.NET Core 3.1, using Entity Framework Core 3.1 as well, and am now trying to Publish it...
Read more >DbCommand failure message doesn't include exception ...
Version: EF Core 5.0 We see in our logs many command error messages. It says "Failed executing DbCommand ..." efcore/src/EFCore.
Read more >Tracking vs. No-Tracking Queries - EF Core
Tracking behavior controls if Entity Framework Core keeps information about an entity instance in its change tracker.
Read more >Handle errors in ASP.NET Core
Discover how to handle errors in ASP.NET Core apps. ... Stack trace; Query string parameters, if any; Cookies, if any; Headers.
Read more >Building End-to-End Diagnostics and Tracing: Trace Context
Behaviors in NServiceBus are similar to ASP.NET Core middleware. You get two parameters, the first being the context of the operation ...
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
Okay, found what’s needed, will get a PR up in a bit
Merged https://github.com/elastic/apm-agent-dotnet/pull/948 - thanks @kanadaj 👏