如何在sql存储过程中打log_教你如何记录Entity Framework框架自动生成的SQL语句

有两种方法可以记录执行的SQl语句:

· 使用DbContext.Database.Log属性

· 实现IDbCommandInterceptor接口

1使用DbContext.Database.Log属性

下面截图显示了Database属性和Log属性,可以看出这个属性是一个委托,类型为Action

0cedf97ce0a9b436e8ca9613e5ec1893.png

对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

///  /// 自定义上下文 ///  [DbConfigurationType(typeof(MySqlEFConfiguration))] public class CustomDbContext : DbContext { public CustomDbContext() : base("name=Master") {  //this.Configuration.LazyLoadingEnabled = false; //new DropCreateDatabaseIfModelChanges() //new DropCreateDatabaseAlways() Database.SetInitializer(null); this.Database.Log = Log; } public DbSet 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); }}

执行结果如下截图

0ea4a32a7765dd84df20640338aa8ea9.png

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.Infrastructure.Interception.DatabaseLogFormatter>)

DatabaseLogFormatter的三个方法

  • LogCommand:在SQL 语句或存储过程执行前记录它。
  • LogParameter:记录参数,默认被LogCommand调用(未能验证这一点)
  • LogResult:记录SQL 语句或存储过程执行后的一些相关信息

这三个方法包含的参数为:

  • DbCommand command:SQL 语句或存储过程相关的信息。
  • DbCommandInterceptionContext interceptionContext:执行结果相关的信息。
  • DbParameter parameter:System.Data.Common.DbCommand 的参数。

重写LogCommand或LogResult都可以改变SQL 语句或存储过程相关信息格式,但是注意这两个方法interceptionContext参数的值可能会不一样。

继承DatabaseLogFormatter,实现自定义格式化器

public class CustomDatabaseLogFormatter : DatabaseLogFormatter { public CustomDatabaseLogFormatter(DbContext context, Action writeAction) : base(context, writeAction) { } public override void LogCommand(DbCommand command, DbCommandInterceptionContext interceptionContext) { } public override void LogResult(DbCommand command, DbCommandInterceptionContext interceptionContext) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < command.Parameters.Count; i++) { sb.AppendLine(string.Format("参数名称:{0},值:{1}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值