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监控,写日志
EF 记录执行的sql语句的更多相关文章
- asp.net EF框架执行原生SQL语句
1.执行无参数sql: string sql = "select * from IntegralInfo where convert(nvarchar,getdate(),23)='{0}' ...
- mysql记录执行的SQL语句
show variables like "general_log%"; SET GLOBAL general_log = 'ON';SET GLOBAL general_log = ...
- EF-记录程序自动生成并执行的sql语句日志
在EntityFramework的CodeFirst模式中,我们想将程序自动生成的sql语句和执行过程记录到日志中,方便以后查看和分析. 在EF的6.x版本中,在DbContext中有一个Databa ...
- EF Core 日志跟踪sql语句
EF Core 日志跟踪sql语句 官方文档链接:https://docs.microsoft.com/en-us/ef/core/miscellaneous/logging 1.新增自定义ILogg ...
- MySql使用show processlist查看正在执行的Sql语句
今天上班例行的查看了下服务器的运行状况,发现服务器特卡,是mysqld这个进程占用CPU到了99%导致的. 比较好奇是那个程序在使用mysql导致cpu这么高的,通过show processlist命 ...
- tcpdump来抓取执行的sql语句
# tcpdump -n -nn -tttt -i eth1 -s 65535 'port 3306' -w tcpdump_mysql.ret -C 100 一个TCP包中包含多个mysql协议包, ...
- IBatis.Net获取执行的Sql语句
前言 IBatis.Net中Sql语句是些在配置文件中的,而且配置文件是在程序启动时读取的(我们开发的时候需要将其设置成较新复制或者是始终复制),而不是程序将其包含在其中(例如NHibernate的映 ...
- django框架 - 实时查看执行的sql语句
django框架采用的ORM模型,我们可以通过mysql的日志记录实时看到执行的sql语句,具体步骤如下: 第一步:找到mysql的配置文件 第二步:编辑mysql配置文件 第三步:重启mysql 第 ...
- Thinkphp 5 调试执行的SQL语句
在模型操作中 ,为了更好的查明错误,经常需要查看下最近使用的SQL语句,我们可以用getLastsql方法来输出上次执行的sql语句.例如: User::get(1); echo User::getL ...
随机推荐
- robotium 测试APK<一> 建立测试工程
1.准备Android开发环境 2.准备签名工具http://www.troido.de/re-sign.jar 1.建立测试工程 打开Eclipse,点击File->New一个Android ...
- DB2 Error Messages (Sorted by SQLCODE)
DB2 Error Messages (Sorted by SQLCODE) DB2 Error Messages (Sorted by SQLCODE) SQLCODE SQLSTATE Descr ...
- Html 5 版 电子时钟
效果图: html 5 canvas元素 Html 5的canvas元素可以用于在网页上绘制图形[即canvas的作用]. canvas画布使用JavaScript在网页上绘制图形 其拥有绘制各种路 ...
- 推荐几个Laravel 后台管理系统
小编推荐几个Laravel 后台管理系统 由百牛信息技术bainiu.ltd整理发布于博客园 一.不容错过的Laravel后台管理扩展包 —— Voyager 简介Voyager是一个你不容错过的La ...
- JSP介绍(2)--- 九大隐式对象
request对象 每当客户端请求一个JSP页面时,JSP引擎就会制造一个新的request对象来代表这个请求. request对象提供了一系列方法来获取HTTP头信息,cookies,HTTP方法等 ...
- 希尔排序(java)
希尔排序是对直接插入排序的一种优化,基本思想是把待排序的数据元素分成若干个小组,对同一小组内的数据元素用直接插入法排序:小组的个数逐次缩小:当完成了所有数据元素都在一个组内的排序后排序过程结束.希尔排 ...
- 提取html里面url()导入的图片正则表达式
之前写过一次,后来无意发现还是有问题,比如 <a onclick="openZoosUrl('chatwin','param'); 这个click事件函数里面就有url()部分,造成了 ...
- 教程:动手用自己电脑搭建一个网站 (nat123 花生壳 动态域名 个人电脑做服务器)
先ps一下..今晚试了N种方法,终于找到一个靠谱 免费 好用的方法,来改装自己电脑成为服务器,在外网也能通过域名访问了!!! 需要准备的东西: Tomcat:这个是web容器,其实有了这个就已经让电脑 ...
- Linux/Unix中的命令提示符prompt
用惯了DOS的伙计刚用Unix时最想干的事情就是想把Unix搞得像DOS一些, 其中的一条就是把Unix的提示符设置成$p$g那样的.下面就说一说做的方法. 不同的SHELL设置的方法不同,比较方便的 ...
- CodeChef - ELHIDARR Find an element in hidden array(二分交互)
Find an element in hidden array There is an array of length N consisting of non-negative integers. T ...