时间:2021-07-01 10:21:17 帮助过:4人阅读
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { optionsBuilder.UseLoggerFactory(MyLoggerFactory).UseMySql("Server=xxx;User Id=xxx;Password=xxx;Database=bebefocus;Persist Security Info=True;"); } }
6. 运行查看 在输出面板里查看
二. 使用 Microsoft.Extensions.Logging.Console 查看生成的SQL语句
1. 利用nuget添加引用Microsoft.Extensions.Logging.Console
2. 在DbContext文件里添加引用
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console;
3. 添加日志工厂
public static readonly LoggerFactory MyLoggerFactory = new LoggerFactory(new[] { new ConsoleLoggerProvider((category, level) => category == DbLoggerCategory.Database.Command.Name&& level == LogLevel.Information, true) });
4. 连接字符串中添加日志
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { optionsBuilder.UseLoggerFactory(MyLoggerFactory).UseMySql("Server=xxx;User Id=xxx;Password=xxx;Database=bebefocus;Persist Security Info=True;"); } }
5. 查看生成的SQL语句
控制台dotnet run运行程序,
执行有查询语句的方法 控制台自动打印出SQL语句
.net core 3.0 利用日志查看ef生成的SQL语句
标签:prot contex 实现 level form image href blog gif