Log Queries executed by Entity Framework DbContext

后端 未结 5 1796
夕颜
夕颜 2020-11-27 16:16

I\'m using EF 6.0 with LINQ in MVC 5 project. I want to log all the SQL queries executed by the Entity Framework DbContext for debugging/performance-measurement purpose.

5条回答
  •  野性不改
    2020-11-27 17:03

    Entity Framework Core 3

    From this article

    Create a factory and set the filter.

    var loggerFactory = LoggerFactory.Create(builder =>
    {
        builder
        .AddConsole((options) => { })
        .AddFilter((category, level) =>
            category == DbLoggerCategory.Database.Command.Name
            && level == LogLevel.Information);
    });
    

    Tell the DbContext to use the factory in the OnConfiguring method:

    optionsBuilder.UseLoggerFactory(_loggerFactory);
    

提交回复
热议问题