在 .NET Core 中如何让 Entity Framework Core 在日志中记录由 LINQ 生成的SQL语句
在開發中,我們想在調試中查看EF Core執行的sql語句,可以使用SQL Studio Manager Tools工具,另一種方式是使用EF Core提供的日志。在ASP.NET Core使用Entity Framework Core的日志.
早在Entity Framework Core1.0 ,使用相關的ILoggerProvider ILogger 這些基礎接口類.來實現過日志記錄.
在Entity Framework Core2.0 ?估計是為了配合ASP.NET Core的日志.所以對這些接口進行了更進一步的包裝,也棄用了一些接口和類,如:IRelationalCommandBuilderFactory ,DbCommandLogData 但是Entity Framework Core2.0 在DbContextOptionsBuilder添加了新的擴展方法.UseLoggerFactory 看到LoggerFactory,研究過ASP.NET Core日志記錄的,應該就很熟悉了..這是ASP.NET Core日志記錄的工廠類. 也就是為什么我們如果在ASP.NET Core中注入自己的日志記錄,也可以通過配置來記錄相關的SQL操作的原因. 這里我們主要是直接只監控EF Core的日志.
using?dotNET.Core;using?Microsoft.Extensions.Logging; using?System; using?System.Collections.Generic; using?System.Diagnostics; using?System.Text;namespace?dotNET.EFCoreRepository {///?<summary>///?ef?日志///?</summary>public?class?EFLoggerProvider?:?ILoggerProvider{public?ILogger?CreateLogger(string?categoryName)?=>?new?EFLogger(categoryName);public?void?Dispose()?{?}}///?<summary>///?///?</summary>public?class?EFLogger?:?ILogger{private?readonly?string?categoryName;public?EFLogger(string?categoryName)?=>?this.categoryName?=?categoryName;public?bool?IsEnabled(LogLevel?logLevel)?=>?true;public?void?Log<TState>(LogLevel?logLevel,?EventId?eventId,?TState?state,?Exception?exception,?Func<TState,?Exception,?string>?formatter){//ef?core執行數據庫查詢時的categoryName為Microsoft.EntityFrameworkCore.Database.Command,日志級別為Informationif?(categoryName?==?"Microsoft.EntityFrameworkCore.Database.Command"&&?logLevel?==?LogLevel.Information){var?logContent?=?formatter(state,?exception);NLogger.Debug(logContent);//TraceMessage("Something?happened.");//??NLogger.Info(GetCodeLineAndFileName());//TODO:?拿到日志內容想怎么玩就怎么玩吧Console.WriteLine();Console.ForegroundColor?=?ConsoleColor.Green;Console.WriteLine(logContent);Console.ResetColor();}}public?IDisposable?BeginScope<TState>(TState?state)?=>?null;public?void?TraceMessage(string?message,[System.Runtime.CompilerServices.CallerMemberName]?string?memberName?=?"",[System.Runtime.CompilerServices.CallerFilePath]?string?sourceFilePath?=?"",[System.Runtime.CompilerServices.CallerLineNumber]?int?sourceLineNumber?=?0 ){NLogger.Debug("message:?"?+?message);NLogger.Debug("member?name:?"?+?memberName);NLogger.Debug("source?file?path:?"?+?sourceFilePath);NLogger.Debug("source?line?number:?"?+?sourceLineNumber);}public?string?GetCodeLineAndFileName(){StackTrace?insStackTrace?=?new?StackTrace(true);var?insStackFrames?=?insStackTrace.GetFrames();string?str?=?"";foreach(var?insStackFrame?in?insStackFrames){str?+=?String.Format("\nFile:?{0},?Line:?{1}\n",?insStackFrame.GetFileName(),?insStackFrame.GetFileLineNumber());}return?str;}} }第一步:添加日志類
第二步:OnConfiguring ?方法添加日志調用
protected?override?void?OnConfiguring(DbContextOptionsBuilder?optionsBuilder){var?loggerFactory?=?new?LoggerFactory();loggerFactory.AddProvider(new?EFLoggerProvider());optionsBuilder.UseLoggerFactory(loggerFactory);base.OnConfiguring(optionsBuilder);}總結
以上是生活随笔為你收集整理的在 .NET Core 中如何让 Entity Framework Core 在日志中记录由 LINQ 生成的SQL语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# Jpush 极光推送消息推送
- 下一篇: N 年沉淀,腾讯这套系统终于开源!