Entity Framework——记录执行的命令信息
有两种方法可以记录执行的SQl语句:
- 使用DbContext.Database.Log属性
- 实现IDbCommandInterceptor接口
一 使用DbContext.Database.Log属性
下面截图显示了Database属性和Log属性,可以看出这个属性是一个委托,类型为Action<string>
对Log属性的解释为:
Set this property to log the SQL generated by the System.Data.Entity.DbContext to the given delegate. For example, to log to the console, set this property to System.Console.Write(System.String).
使用方法:
1)在自定义上下文中获得执行的SQL相关信息,即在自定上下文的构造函数中使用Database.Log
/// <summary>
/// 自定义上下文
/// </summary>
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class CustomDbContext : DbContext
{
public CustomDbContext()
: base("name=Master")
{
//this.Configuration.LazyLoadingEnabled = false;
//new DropCreateDatabaseIfModelChanges<CustomDbContext>()
//new DropCreateDatabaseAlways<CustomDbContext>()
Database.SetInitializer<CustomDbContext>(null);
this.Database.Log = Log;
}
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
EntityConfiguration.Set(modelBuilder);
}
private void Log(string cmd)
{
//或输出到控制台
//Console.Write(cmd);
//或输出到文件
//using (StreamWriter sw = new StreamWriter(@"E:EFCmdLogger.txt"))
//{
// sw.WriteLine(cmd);
//}
//或输出到调试信息窗口
Debug.WriteLine(cmd);
}
}
执行结果如下截图
2)在具体的方法中使用
public class EFOPerations
{
public static void ReadUser()
{
Stopwatch stw = new Stopwatch();
stw.Start();
using (CustomDbContext db = new CustomDbContext())
{
db.Database.Log = Console.WriteLine;
User user = db.Users.Find(1);
var userDTO = new { Account = user.Account };
}
stw.Stop();
var time = stw.ElapsedMilliseconds;
}
}
注意
db.Database.Log = Console.WriteLine;这条语句的位置;如果将其放到查询语句,即User user = db.Users.Find(1);之后则无法输出信息!
还可以改变日志的格式:
创建继承自DatabaseLogFormatter的类,实现新的格式化器,然后使用
System.Data.Entity.DbConfiguration.SetDatabaseLogFormatter(System.Func<System.Data.Entity.DbContext,System.Action<System.String>,System.Data.Entity.Infrastructure.Interception.DatabaseLogFormatter>)
DatabaseLogFormatter的三个方法
LogCommand:在SQL 语句或存储过程执行前记录它。
LogParameter:记录参数,默认被LogCommand调用(未能验证这一点)
LogResult:记录SQL 语句或存储过程执行后的一些相关信息
这三个方法包含的参数为:
DbCommand command:SQL 语句或存储过程相关的信息。
DbCommandInterceptionContext<TResult> interceptionContext:执行结果相关的信息。
DbParameter parameter:System.Data.Common.DbCommand 的参数。
重写LogCommand或LogResult都可以改变SQL 语句或存储过程相关信息格式,但是注意这两个方法interceptionContext参数的值可能会不一样。
继承DatabaseLogFormatter,实现格式化器
public class CustomDatabaseLogFormatter : DatabaseLogFormatter
{
public CustomDatabaseLogFormatter(DbContext context, Action<string> writeAction)
: base(context, writeAction)
{
}
public override void LogCommand<TResult>(DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
{
}
public override void LogResult<TResult>(DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < command.Parameters.Count; i++)
{
sb.AppendLine(string.Format("参数名称:{0},值:{1}", command.Parameters[0].ParameterName, command.Parameters[0].Value));
}
Write(command.CommandText + Environment.NewLine
+ command.CommandTimeout + Environment.NewLine
+ command.CommandType + Environment.NewLine
+ Environment.NewLine
+ sb.ToString());
}
}
设置新的格式化器
public class CustomDbConfiguration : MySqlEFConfiguration
{
public CustomDbConfiguration():base()
{
//this.AddInterceptor(new CommandInterceptor(new Logger()));
SetDatabaseLogFormatter((context, writeAction) => new CustomDatabaseLogFormatter(context, writeAction));
}
}
使用自定义CustomDbConfiguration
[DbConfigurationType(typeof(CustomDbConfiguration))]
public class CustomDbContext : DbContext
{
public CustomDbContext()
: base("name=Master")
{
//this.Configuration.LazyLoadingEnabled = false;
//new DropCreateDatabaseIfModelChanges<CustomDbContext>()
//new DropCreateDatabaseAlways<CustomDbContext>()
Database.SetInitializer<CustomDbContext>(null);
this.Database.Log = Log;
}
......
}
二 实现IDbCommandInterceptor接口
实现IDbCommandInterceptor,同时为了灵活的记录执行信息,定义了日志接口
public class CommandInterceptor : IDbCommandInterceptor
{
private ICommandLogger logger;
public CommandInterceptor(ICommandLogger logger)
{
this.logger = logger;
}
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
this.logger.Log<int>(command, interceptionContext);
}
public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
this.logger.Log<int>(command, interceptionContext);
}
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
{
this.logger.Log<DbDataReader>(command, interceptionContext);
}
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
{
this.logger.Log<DbDataReader>(command, interceptionContext);
}
public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
this.logger.Log<object>(command, interceptionContext);
}
public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
this.logger.Log<object>(command, interceptionContext);
}
}
public interface ICommandLogger
{
void Log<T>(DbCommand command, DbCommandInterceptionContext<T> interceptionContext);
}
public class Logger : ICommandLogger
{
public void Log<T>(System.Data.Common.DbCommand command, System.Data.Entity.Infrastructure.Interception.DbCommandInterceptionContext<T> interceptionContext)
{
StringBuilder sb = new StringBuilder();
for(int i =0;i<command.Parameters.Count;i++)
{
sb.AppendLine(string.Format("参数名称:{0},值:{1}", command.Parameters[0].ParameterName, command.Parameters[0].Value));
}
Debug.WriteLine(command.CommandText+Environment.NewLine
+ command.CommandTimeout + Environment.NewLine
+ command.CommandType + Environment.NewLine
+ Environment.NewLine
+ sb.ToString());
}
}
如何使用这两个类呢?
1使用配置文件
<entityFramework>
<interceptors>
<interceptor type="ConsoleApp_EntityFramework.Interceptor.CommandInterceptor, ConsoleApp_EntityFramework.Interceptor">
</interceptor>
</interceptors>
</entityFramework>
但是采用这种方式要对上面的CommandInterceptor 进行改造。
public class CommandInterceptor : IDbCommandInterceptor
{
private ICommandLogger logger;
public CommandInterceptor()
{
this.logger = new Logger();
}
......
}
但是如果EF操作的是Mysql那么这种方法不行,抛出异常:无法识别的元素“interceptors”
2编码方式
只有上面两个类还不够,还要定义创建一个继承自DbConfiguration的配置类
public class CustomDbConfiguration : DbConfiguration
{
public CustomDbConfiguration():base()
{
this.AddInterceptor(new CommandInterceptor(new Logger()));
}
}
在自定义数据库上下文上使用此特性
/// <summary>
/// 自定义上下文
/// </summary>
[DbConfigurationType(typeof(CustomDbConfiguration))]
public class CustomDbContext : DbContext
{
......
}
一切准备好后运行程序,却抛出异常:
The ADO.NET provider with invariant name 'MySql.Data.MySqlClient' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details.
似乎是MySql.Data.MySqlClient的问题,其实不是!
如果是SQL Server则没问题,但这里EF框架操作的是MySql,要是使用MySql.Data.Entity.MySqlEFConfiguration这个类,而不是System.Data.Entity.DbConfiguration,所以CustomDbConfiguration应该派生自MySql.Data.Entity.MySqlEFConfiguration
public class CustomDbConfiguration : MySqlEFConfiguration
{
public CustomDbConfiguration():base()
{
this.AddInterceptor(new CommandInterceptor(new Logger()));
}
.....
}
这样修改后,运行程序得到下面的结果:
可以看到日志打印了两次,这是因为ReaderExecuting和ReaderExecuted各调用了一次,执行的顺序是先ReaderExecuting然后ReaderExecuted。
-----------------------------------------------------------------------------------------
转载与引用请注明出处。
时间仓促,水平有限,如有不当之处,欢迎指正。
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Codeforces Round #615 (Div. 3) F. Three Paths on a Tree
- Matlab 内联函数及匿名函数
- 渗透测试 | 内网信息收集
- 浅谈 CSS 的用法
- PAT (Advanced Level) Practice 1049 Counting Ones (30分)
- 用递归的思想实现二叉树前、中、后序迭代遍历
- 利用WAF进行拒绝服务攻击
- leetcode树之从根到叶的二进制数之和
- Vue3 模板编译原理
- leetcode树之从上到下打印二叉树
- leetcode树之二叉搜索树的最近公共祖先
- leetcode树之从翻转二叉树
- Linux常见的持久化后门汇总
- 一篇文章带你使用 Python搞定对 Excel 表的读写和处理(xlsx文件的处理)
- 从 rollup 初版源码学习打包原理