如何使用 Entity Framework 7 记录查询? [英] How to log queries using Entity Framework 7?
问题描述
我在夜间构建频道上使用 Entity Framework 7(现在我使用的是 EntityFramework.7.0.0-beta2-11524 版本)并且我正试图记录 EF 出于好奇而生成的查询.
I am using Entity Framework 7 on the nightly build channel (right now I'm using version EntityFramework.7.0.0-beta2-11524) and I'm trying to log the queries that EF generates just out of curiosity.
我正在编写一个简单的控制台程序,我尝试使用 相同的日志记录技术 EF6 使用,但 DbContext.Database.Log
在 Entity Framework 7 上不可用.有没有办法记录日志或只是看一眼 EF7 生成的 SQL?
I'm writing a simple console program, I tried using the same logging technic that EF6 uses, but DbContext.Database.Log
is not available on Entity Framework 7. Is there a way to log or just take a peek at the SQL generated by EF7?
推荐答案
对于那些使用 EF7 的人,上述方法都不适合我.但这就是我让它工作的方式.(来自@avi 樱桃的评论)
For those using EF7 none of the above worked for me. But this is how i got it working. (from @avi cherry's comment)
在您的 Startup.cs 中,您可能有一个包含大量配置的 Configure 方法.它应该如下所示(除了你的东西).
In your Startup.cs you proably have a Configure method with a bunch of configurations in it. It should look like below (in addition to your stuff).
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
//this is the magic line
loggerFactory.AddDebug(LogLevel.Debug); // formerly LogLevel.Verbose
//your other stuff
}
这篇关于如何使用 Entity Framework 7 记录查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!