在Entity Framework Core中启用原始SQL日志记录 [英] Enable raw SQL logging in Entity Framework Core

查看:290
本文介绍了在Entity Framework Core中启用原始SQL日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何启用DbCommand原始SQL查询的日志记录?

How do I enable the logging of DbCommand raw SQL queries?

我已将以下代码添加到Startup.cs文件中,但是看不到任何日志条目

I have added the following code to my Startup.cs file, but do not see any log entries from the Entity Framework Core.

void ConfigureServices(IServiceCollection services)
{
    services.AddLogging();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug(LogLevel.Debug);
}

我希望看到这样的东西:

I'm expecting to see something like this:

Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilder...
SELECT [t].[Id], [t].[DateCreated], [t].[Name], [t].[UserName]
FROM [Trips] AS [t]


推荐答案

想通了-需要配置DbContext才能使用记录器工厂。

Figured it out - need to configure DbContext to use logger factory.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    base.OnConfiguring(optionsBuilder);

    optionsBuilder.UseLoggerFactory(_loggerFactory);
}

这篇关于在Entity Framework Core中启用原始SQL日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆