zoukankan      html  css  js  c++  java
  • 关于EF输出sql的执行日志

    sqlserver中可以使用sql profiler;但是mysql当中无法查看;只能借助于组件;

    ADO.NET Entity Framework CodeFirst 如何输出日志(EF4.3) 用的EFProviderWrappers ,这个组件好久没有更新了,对于SQL执行日志的解决方案的需求是杠杠的,今天给大家介绍一个更好的组件Clutch.Diagnostics.EntityFramework,可以通过Nuget 获取:

    image

    这个框架定义了一个接口 IDbTracingListener:

    namespace Clutch.Diagnostics.EntityFramework
    {
    
      public interface IDbTracingListener
      {
    
        void CommandExecuting(DbTracingContext context);
    
        void CommandFinished(DbTracingContext context);
    
        void ReaderFinished(DbTracingContext context);
    
        void CommandFailed(DbTracingContext context);
    
        void CommandExecuted(DbTracingContext context);
    
      }
    }
    实现这个接口,添加一个类,里面实现自己的SQL 日志记录:
    using System;
    using System.Data.Common;
    using System.Diagnostics;
    using Clutch.Diagnostics.EntityFramework;
    
    /// <summary>
    /// Implementation of IDbTracingListener Class is used for tracing all SQL Queries to the entity framework database
    /// </summary>
    public class DbTracingListener : IDbTracingListener
    {
        public void CommandExecuted(DbConnection connection, DbCommand command, object result, TimeSpan duration)
        {
            Debug.WriteLine(command.CommandText);
            Debug.WriteLine(string.Format("Executed in: {0}", duration));
        }
    
        public void CommandExecuting(DbConnection connection, DbCommand command)
        {
    
        }
    
        public void CommandFailed(DbConnection connection, DbCommand command, Exception exception, TimeSpan duration)
        {
    
        }
    
        public void CommandFinished(DbConnection connection, DbCommand command, object result, TimeSpan duration)
        {
    
        }
    }

    在方法内部通过 context.Command.CommandText 可以获得你的ef的sql命令的内容。

    然后在程序的入口启用SQL日志输出实现:

    // Enable Tracing queries
    DbTracing.Enable();
    // Adding the listener (implementation of IDbTracingListener)
    DbTracing.AddListener(new DbTracingListener());
  • 相关阅读:
    activiti基础二
    activiti基础一
    sql中between包含边界值吗?
    Mybatis遍历list,Array,map
    元祖变成字符串 || 字符串中替换部分字符串 || enumberate可以找到字符串,列表,元祖的的索引和值 || zip的用法 || 判断变量的类型
    py怎样建立模块和怎样导入模块
    怎样判断变量是数组?
    oracle中字符串连接用||
    将对象的所有属性名放到一个数组中 || 获得对象的所有属性名 || return;不具有原子性 || 怎样自己制作异常|| 判断对象有没有某个属性 || 当传递的参数比需要的参数少的时候,没有的值会被赋予undefined || 获得函数实际传递的参数 || 怎么用函数处理一个对象 || 用一个名字空间定义一个模块所有的函数 || 给一个对象添加方法
    Bootstrap-table 相关参数
  • 原文地址:https://www.cnblogs.com/tianboblog/p/4774231.html
Copyright © 2011-2022 走看看