EF Core如何输出日志到Visual Studio的输出窗口
我们在使用EF Core的时候,很多时候需要在Visual Studio的输出窗口中知道EF Core在后台生成的SQL语句是什么,这个需求可以通过自定义EF Core的ILoggerFactory和ILogger类来实现:
首先定义一个实现了ILogger接口的类EFLogger,主要目的是将EF Core生成的Log信息输出到Visual Studio的输出窗口:
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics; namespace WebCore.Utils
{
public class EFLogger : ILogger
{
protected string categoryName; public EFLogger(string categoryName)
{
this.categoryName = categoryName;
} public IDisposable BeginScope<TState>(TState state)
{
return null;
} public bool IsEnabled(LogLevel logLevel)
{
return true;
} public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
//通过Debugger.Log方法来将EF Core生成的Log信息输出到Visual Studio的输出窗口
Debugger.Log(, categoryName, "=============================== EF Core log started ===============================\r\n");
Debugger.Log(, categoryName, formatter(state,exception)+"\r\n");
Debugger.Log(, categoryName, "=============================== EF Core log finished ===============================\r\n");
}
}
}
然后定义一个实现了ILoggerFactory接口的类EFLoggerFactory,用于创建上面定义的EFLogger类的实例:
using Microsoft.Extensions.Logging; namespace WebCore.Utils
{
public class EFLoggerFactory : ILoggerFactory
{
public void AddProvider(ILoggerProvider provider)
{
} public ILogger CreateLogger(string categoryName)
{
return new EFLogger(categoryName);//创建EFLogger类的实例
} public void Dispose()
{ }
}
}
最后在DbContext的OnConfiguring方法中,调用optionsBuilder.UseLoggerFactory来将EFLoggerFactory类的实例注入给EF Core,这样所有DbContext的Log信息,都会由EFLogger类输出到Visual Studio的输出窗口了。
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using WebCore.Utils; namespace WebCore.Entities
{
public partial class TestDBContext : DbContext
{
public TestDBContext()
{
this.Database.SetCommandTimeout();//设置SqlCommand永不超时
} public TestDBContext(DbContextOptions<TestDBContext> options)
: base(options)
{
} public virtual DbSet<Person> Person { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseLoggerFactory(new EFLoggerFactory());//将EFLoggerFactory类的实例注入给EF Core,这样所有DbContext的Log信息,都会由EFLogger类输出到Visual Studio的输出窗口了
optionsBuilder.UseSqlServer("Server=localhost;User Id=sa;Password=1qaz!QAZ;Database=TestDB");
}
} protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//省略代码...
}
}
}
注意OnConfiguring这个方法是virtual的,所以我们也可以选择在DbContext的子类中来重写这个方法,在子类的OnConfiguring中注入EFLoggerFactory类的实例到DbContext。
最后我们来看看EF Core日志输出的效果,还是很规整的,最重要的是我们可以实时的得到EF Core在后台生成的SQL语句:

EF Core如何输出日志到Visual Studio的输出窗口的更多相关文章
- Visual Studio的输出窗口上输出调试信息的函数
Visual Studio的输出窗口上输出文字的函数 参考网站:http://www.voidcn.com/blog/u011808175/article/p-2083567.html 当你编写非控制 ...
- 安装软件(名称不记得了)后,系统开机提示 visual studio just-in-time debugger窗口(WINDOWS错误提示框)
出现这种情况,往往是因为原先安装有VS,后来因某些原因(比如:卸载)导致VS无法使用!!当系统中的有些软件出现错误时,会自动调用vs进行调试,但因为VS无法使用,就出现了visual studio j ...
- log4net日志输出配置即输出到文件又输出到visual studio的output窗口
<configuration> <configSections> <section name="log4net" type="log4net ...
- ASP.NET Core + Angular 2 Template for Visual Studio
多个月以来,我和多个Github上的社区贡献者一起建立支持库.包,我们最终的目的是希望完成这样一个作为起点的模板,也就是基于把Typescript代码和Angular2宿主在ASP.NET Core项 ...
- .NET CORE 实践(3)--Visual Studio 2015 Update 3更新之后DotNetCore.1.0.1-VS2015Tools.Preview2.0.2.exe无法正确安装
打开 https://www.microsoft.com/net/core#windows,点击 https://go.microsoft.com/fwlink/?LinkId=691129下载vs2 ...
- .NET 6.0.6 和 .NET Core 3.1.26、Visual Studio 2022 17.2 和 17.3 Preview 2 和 .NET 7.0 Preview 5 同时发布
Microsoft 昨天发布了适用于 .NET 6.0.6 和 .NET Core 3.1.26.NuGet.Visual Studio 2019 和 Visual Studio 2022 17.2 ...
- visual studio code 输出乱码
问题: 解决方法: 首先,这个与VS本身无关,问题是出现在windows的dos显示设置上. 如何解决这个问题? 1.打开运行,输入cmd: 2.界面顶部右键,选择默认值: 3.将437(OEM-美国 ...
- Working with Data » 使用Visual Studio开发ASP.NET Core MVC and Entity Framework Core初学者教程
原文地址:https://docs.asp.net/en/latest/data/ef-mvc/intro.html The Contoso University sample web applica ...
- 【翻译】使用Visual Studio创建Asp.Net Core MVC (一)
This tutorial will teach you the basics of building an ASP.NET Core MVC web app using Visual Studio ...
随机推荐
- webservice使用EF生成的model序列化问题
类型 xx 的成员 xxx 是接口,因此无法将其序列化. 修改.tt模板文件,添加以下标红两行 <# foreach (var navigationProperty in navigationP ...
- Express (Routing、Middleware、托管静态文件、view engine 等等)
1. Express 简介 Express 是基于 Node.js 平台,快速.开放.极简的 web 开发框架,它提供一系列强大的特性,帮助你创建各种 Web 和移动设备应用. Express 不对 ...
- 003客户端负载均衡Ribbon & 短路器Hystrix
1.POM配置 和普通Spring Boot工程相比,仅仅添加了Eureka.Ribbon.Hystrix依赖和Spring Cloud依赖管理 <dependencies> <!- ...
- Java从入门到精通——数据库篇Mongo DB 安装启动及配置详解
一.概述 Mongo DB 下载下来以后我们应该如何去安装启动和配置才能使用Mongo DB,本篇博客就给大家讲述一下Mongo DB的安装启动及配置详解. 二.安装 1.下载Mongo DB ...
- CentOS 7运维管理笔记(12)----GUI配置工具Webmin的安装
早期的Linux系统管理员或是Web管理员在修改服务器配置时使用最多的就是vi编辑器,但是现在越来越多的基于GUI界面的配置工具出现了,毕竟人们还是喜欢以直接的可视化的方式来修改服务器的配置,而不是再 ...
- sqlserver查询当月数据
SELECT * FROM table WHERE datediff(month,LoginTime,getdate())=0 ORDER BY LoginTime SELECT * FROM tab ...
- linux第一个C语言和sh脚本
linux第一个C语言 $ gedit hello_world.c #include <stdio.h> int main(void) { printf("hello world ...
- 网站的Information Architecture--构建一个最优用户体验的site structure
http://conversionxl.com/website-information-architecture-optimal-user-experience/ 在网站上应该有什么类型的conten ...
- percona toolkit之slave工具
1:pt-slave-find ,主要是查找MySQL的层级,其实我感觉这个用处不是很大,因为层级比较多架构本身就很少,查看从库的话一般情况我们可以通过show slave hosts查看(不过不能显 ...
- [问题记录]cocos2dx编译打包apk过程&问题记录
目录: 1. 入门 2. 编译 3. 问题 4. 总结 5. 参考 ------------------------------------------------------------------ ...