当前位置:网站首页>EF Core 2.2中将ORM框架生成的SQL语句输出到控制台
EF Core 2.2中将ORM框架生成的SQL语句输出到控制台
2022-07-31 15:50:00 【极客研究者】
看了下杨中科老师的书,EF Core 5.0很简单的一行代码就可以把生成的SQL输出到控制台中。
查了下微软官方文档:


protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.LogTo(Console.WriteLine);但低版本的不适合,我们公司的项目是基于2.2版本的。于是搜寻了下。
有一篇文章可以使用,但是里面的方法已被弃用了,不推荐使用
ConsoleLoggerProvider这个类。
有个老外分享是比较正确的使用姿势:
How to create a LoggerFactory with a ConsoleLoggerProvider?

还是推荐使用依赖注入的方式.
在公司的项目中进行整合:
public void ConfigureServices(IServiceCollection services)
{
.................................
.................................
.................................
.................................
.................................
.................................
.................................
services.AddLogging(builder => builder
.AddConsole()
.AddFilter(level => level >= LogLevel.Information));
}public class XXXXDbContext
{
private readonly IConfiguration _configuration;
private readonly ILoggerFactory _loggerFactory;
public XXXXDbContext() : this(ConfigurationHelper.Configuration) { }
public InstacartDbContext(IConfiguration configuration)
{
_configuration = configuration;
}
public InstacartDbContext(IConfiguration configuration, ILoggerFactory loggerFactory)
{
_configuration = configuration;
_loggerFactory = loggerFactory;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseLoggerFactory(_loggerFactory).UseSqlServer("ConnectString"));
}
}
}
边栏推荐
- ansible学习笔记02
- Why is the field of hacking almost filled with boys?
- 【MySQL】Mysql范式及外键作用
- update data table update
- Stuck in sill idealTree buildDeps during npm installation, npm installation is slow, npm installation is stuck in one place
- 为什么黑客领域几乎一片男生?
- t-sne 数据可视化网络中的部分参数+
- 国内市场上的BI软件,到底有啥区别
- ansible study notes 02
- ASP.NET Core generates continuous Guid
猜你喜欢

国内市场上的BI软件,到底有啥区别

WPF project - basic usage of controls entry, you must know XAML

i.MX6ULL驱动开发 | 33 - NXP原厂网络设备驱动浅读(LAN8720 PHY)

Visualize GraphQL schemas with GraphiQL

mongo enters error

6-22 Vulnerability exploit - postgresql database password cracking

t-sne 数据可视化网络中的部分参数+

radiobutton的使用

Qt实战案例(54)——利用QPixmap设计图片透明度

01 邂逅typescript,环境搭建
随机推荐
t-sne 数据可视化网络中的部分参数+
在资源管理类中提供对原始资源的访问——条款15
gerrit中如何切换远程服务器
Matlab matrix basic operations (definition, operation)
11 pinia使用
字符串反转的实现方法总结「建议收藏」
mysql black window ~ build database and build table
type of timer
radiobutton的使用
Emmet 语法
【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
Emmet syntax
第05章 存储引擎【1.MySQL架构篇】【MySQL高级】
C language "the third is" upgrade (mode selection + AI chess)
jeecg master-slave database read-write separation configuration "recommended collection"
Dialogue with Zhuang Biaowei: The first lesson of open source
Handling write conflicts under multi-master replication (4) - multi-master replication topology
Deployment application life cycle and Pod health check
定时器的类型
7. Summary of common interview questions