在Entity Framework Core中启用原始SQL日志记录

编程入门 行业动态 更新时间:2024-10-15 10:17:50
本文介绍了在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日志记录

本文发布于:2023-11-16 15:19:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605463.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:原始   日志   Framework   Entity   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!