1. 程式人生 > >EF 記錄執行的sql語句

EF 記錄執行的sql語句

最近做了箇中等的專案,資料不會很多,開發時間比較緊迫,所以用了EF的框架。

在使用過程中,發現有時候執行的結果不如預期,想看看執行的sql語句為何,遍查詢資料,在網上找到了相關輔助類,拿來使用,部署到生產環境。

程式碼如下:

    public class EFIntercepterLogging : DbCommandInterceptor
    {

        ILog log = LogManager.GetLogger("InfoAppender");
        private readonly Stopwatch _stopwatch = new Stopwatch();
        
public override void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext) { SaveCmdSql(command); base.ScalarExecuting(command, interceptionContext); _stopwatch.Restart(); } public
override void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext) { _stopwatch.Stop(); SaveCmdSql(command); if (interceptionContext.Exception != null) { Trace.TraceError(
"Exception:{1} rn --> Error executing command: {0}", command.CommandText, interceptionContext.Exception.ToString()); } else { Trace.TraceInformation("rn執行時間:{0} 毫秒rn-->ScalarExecuted.Command:{1}rn", _stopwatch.ElapsedMilliseconds, command.CommandText); } base.ScalarExecuted(command, interceptionContext); } public override void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { SaveCmdSql(command); base.NonQueryExecuting(command, interceptionContext); _stopwatch.Restart(); } public override void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { _stopwatch.Stop(); SaveCmdSql(command); if (interceptionContext.Exception != null) { Trace.TraceError("Exception:{1} rn --> Error executing command:rn {0}", command.CommandText, interceptionContext.Exception.ToString()); } else { Trace.TraceInformation("rn執行時間:{0} 毫秒rn-->NonQueryExecuted.Command:rn{1}", _stopwatch.ElapsedMilliseconds, command.CommandText); } base.NonQueryExecuted(command, interceptionContext); } /// <summary> /// 讀取操作 /// </summary> /// <param name="command"></param> /// <param name="interceptionContext"></param> public override void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext) { SaveCmdSql(command); base.ReaderExecuting(command, interceptionContext); _stopwatch.Restart(); } public override void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext) { _stopwatch.Stop(); SaveCmdSql(command); if (interceptionContext.Exception != null) { Trace.TraceError("Exception:{1} rn --> Error executing command:rn {0}", command.CommandText, interceptionContext.Exception.ToString()); } else { Trace.TraceInformation("rn執行時間:{0} 毫秒 rn -->ReaderExecuted.Command:rn{1}", _stopwatch.ElapsedMilliseconds, command.CommandText); } base.ReaderExecuted(command, interceptionContext); } //儲存執行的sql語句 public void SaveCmdSql(DbCommand command) { log.Info(command.CommandText); } }

 

程式碼比較容易理解,重點在 DbCommandInterceptor 這個類.

msdn 連結:https://docs.microsoft.com/zh-cn/dotnet/api/system.data.entity.infrastructure.interception.dbcommandinterceptor?view=entity-framework-6.2.0

 

需要Global.asax中全域性註冊該方法,才能使用

  DbInterception.Add(new EFIntercepterLogging());//註冊EF監控,寫日誌