question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EF Core trace stacktraces don't extend into the application

See original GitHub issue

Describe 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:

  1. Configure Elastic APM for ASP.NET Core and EF Core
  2. Execute some action with EF Core queries in it
  3. 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:closed
  • Created 3 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
kanadajcommented, Sep 1, 2020

Okay, found what’s needed, will get a PR up in a bit

0reactions
gregkalaposcommented, Sep 1, 2020
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found