In the old (pre .net core) era's entity framework 6 as shown in this blog post there is a way to configure an interceptor which can log all slow queries including a stack backtrace.
[ NOTE: In Entity Framework Core prior to version 3.0 this was not possible, thus the original question asked what to do instead. Since the time this question was asked, new options and new versions of EF Core have been released. This question is historical now in nature, and some of the answers that were added later reference other newer versions of EF Core, where interceptors may have been reintroduced, to achieve feature parity with the pre-core era entity framework ]
A question from 2015 about an earlier beta of what was then called EF7, suggests that it was not possible yet in asp.net vnext early betas.
Yet, the whole design of EF Core is to be composable, and in discussions on github bug tracker here that a technique might be possible where you subclass some low level class like SqlServerConnection
and then override some method in there, to get some points you could hook before and after a query is executed, and add some low level logging if a millisecond timer value was executed.
(Edit: References to pre-release information from 2015 removed in 2020)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…