个人可以做网站维护吗,专业团队电脑壁纸,个人网站开发开题报告,郑州 手机网站制作今天我们来聊聊EF的日志记录. 一个好的数据库操作记录不仅仅可以帮你记录用户的操作, 更应该可以帮助你获得效率低下的语句来帮你提高运行效率 废话不多说,我们开始 环境和相关技术 本文采用的环境与技术 系统:WIN7 数据库:SQL Server2008 相关技术:MVC5 EF6.0 简单的记录 … 今天我们来聊聊EF的日志记录. 一个好的数据库操作记录不仅仅可以帮你记录用户的操作, 更应该可以帮助你获得效率低下的语句来帮你提高运行效率 废话不多说,我们开始 环境和相关技术 本文采用的环境与技术 系统:WIN7 数据库:SQL Server2008 相关技术:MVC5 EF6.0 简单的记录 一、修改配置文件 我们先来看看最简化的EF日志记录,任何代码都不用改,在你的配置文件中加入如下配置即可自动记录: 在你的EntityFramework节点下加入如下配置即可(这里需要注意的是第一个参数是你日志的输出地址): interceptorsinterceptor typeSystem.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFrameworkparametersparameter valueD:\ttt\log.txt /parameter valuetrue typeSystem.Boolean //parameters/interceptor/interceptors 我们到对应的地址下就能找相关的日志文件了如下: 二、简单封装: 编写一个自己的DBContext的基类如下: public class DataBaseContextT : DbContext where T:class,new()
{ //重写SaveChanges方法public override int SaveChanges(){ string sql ; //记录实体操作日志this.Database.Log (a) {sql a;}; //这里的sql就是操作日志了.return base.SaveChanges();}
} 通过低级监听接口来进行监听 如果你只是想单纯的记录,上面两种方式应该就能满足你了. 我们记录的目的其实最重要的还是在于分析性能 下面就开始我们的重头戏. 采用IDbCommandInterceptor接口进行EF的监听 首先我们来看看这个接口里面到底有些什么: 写过ADO.NET的人 应该对这些单词很熟悉了吧.(因为EF最终访问数据库的方式还是用的ADO.NET) 注意:每个执行都有ed(执行完成后的监听)和ing(执行时的监听) 下面我们来一步一步实现这个接口 首先定义一个类(名字你随意): //名字可以随意,但是肯定要继承我们的监听接口 - - , public class DatabaseLogger : IDbCommandInterceptor
{
} 然后我们继续, 定义一个静态只读的ConcurrentDictionary作为我们的记录仓储,考虑到数据访问时多线程的情况很常见,所以我们采用线程安全的ConcurrentDictionary 代码如下: public class DatabaseLogger : IDbCommandInterceptor{ static readonly ConcurrentDictionaryDbCommand, DateTime MStartTime new ConcurrentDictionaryDbCommand, DateTime();} 接下来,我们来实现我们所需要的两个方法 一个为onStart来记录SQL语句执行开始的时间 如下: //记录开始执行时的时间private static void OnStart(DbCommand command){MStartTime.TryAdd(command, DateTime.Now);} 然后实现我们的log方法来记录相关的SQL语句和错误信息 rivate static void LogT(DbCommand command, DbCommandInterceptionContextT interceptionContext)
{DateTime startTime;TimeSpan duration; //得到此command的开始时间MStartTime.TryRemove(command, out startTime); if (startTime ! default(DateTime)){duration DateTime.Now - startTime;} elseduration TimeSpan.Zero; var parameters new StringBuilder(); //循环获取执行语句的参数值foreach (DbParameter param in command.Parameters){parameters.AppendLine(param.ParameterName param.DbType param.Value);} //判断语句是否执行时间超过1秒或是否有错if (duration.TotalSeconds 1 || interceptionContext.Exception!null){ //这里编写记录执行超长时间SQL语句和错误信息的代码} else{ //这里编写你自己记录普通SQL语句的代码} } 既然我们已经得到了想要的东西,那具体的记录方式,各位仁者见仁 智者见智 就随意了,所以我这就不写了. 然后接着,我们要实现这个接口的6个方法,如下: public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContextint interceptionContext)
{Log(command, interceptionContext); } public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContextint interceptionContext){OnStart(command);} public void ReaderExecuted(DbCommand command, DbCommandInterceptionContextDbDataReader interceptionContext){Log(command, interceptionContext);} public void ReaderExecuting(DbCommand command, DbCommandInterceptionContextDbDataReader interceptionContext){OnStart(command);} public void ScalarExecuted(DbCommand command, DbCommandInterceptionContextobject interceptionContext){Log(command, interceptionContext);} public void ScalarExecuting(DbCommand command, DbCommandInterceptionContextobject interceptionContext){ OnStart(command);} 其实很简单,就是所有的ing执行我们之前写的OnStart方法,所有的ed执行我们的log方法即可. 接下来,我们需要注入这个接口: 这里我的Demo用的MVC所以我就在 Application_Start()中直接注入了,如下: protected void Application_Start()
{ //注入自己写的监听DbInterception.Add(new MiniProfiler_EFModel.DatabaseLogger());
} 这样我们就完成了整个监听的过程了~ 实现效果如下: 我们得到了执行的秒数 得到了执行的SQL语句: 得到了SQL语句所对应的参数: 大功告成! 写在最后 这里我只是帮各位通过监听来获取到相关的信息,具体如何优化,应该用什么东西进行记录,我就不过多的赘述,这是属于仁者见仁智者见智的东西,不过有兴趣的可以通过博客加我QQ进行讨论.欢迎. 相关文章 采用EntityFramework.Extended 对EF进行扩展(Entity Framework 延伸系列2)第一篇 Entity Framework Plus 之 Audit第二篇 Entity Framework Plus 之 Query Future第三篇 Entity Framework Plus 之 Query Cache第四篇 Entity Framework Plus 之 Batch OperationsEntity Framework教程(第二版)Dapper、Entity Framework 和混合应用.NET Core 1.0、ASP.NET Core 1.0和EF Core 1.0简介 原文地址http://www.cnblogs.com/GuZhenYin/p/5556732.html .NET社区新闻深度好文微信中搜索dotNET跨平台或扫描二维码关注 赞赏 人赞赏