zoukankan      html  css  js  c++  java
  • 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监控,写日志

  • 相关阅读:
    [JXOI2018]游戏
    UltraISO制作启动盘安装CentOS7
    龙芯、鲲鹏、飞腾等国产平台的浏览器
    浏览器相关性能测试网址
    有没有一种组合字体,中文是宋体,英文是times new roman?
    中国航天科工集团第二研究院706所招聘
    华为云
    面试阿里P6难在哪?(面试难点)
    Linux下配置环境变量—— .bashrc 和 /etc/profile
    Linux 内存 占用较高问题排查
  • 原文地址:https://www.cnblogs.com/liuruitao/p/10049072.html
Copyright © 2011-2022 走看看