c# - Entity Framework 6 - 计时查询

标签 c# sql-server entity-framework entity-framework-6

我正在使用 Entity Framework 6,它具有出色的数据库拦截器功能,可以记录从应用程序发送到数据库的查询。但是,我正在努力为这些查询计时,我有一个长时间运行的查询,它返回数十万到数百万行,因此它需要大约 6 到 15 秒,具体取决于该查询将返回的数据量。 Entity Framework 正在返回一个 SqlDataReader,因此我无法获得获取结果所需的确切时间。我想知道从发送查询到读取最后一行的完整执行时间。有什么办法可以做到吗。

最佳答案

这是我通常用于 EF 的记录器。

public class EFLoggerForTesting : IDbCommandInterceptor
{
    static readonly ConcurrentDictionary<DbCommand, DateTime> m_StartTime = new ConcurrentDictionary<DbCommand, DateTime>();

    public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
    {
        Log(command, interceptionContext);
    }

    public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        Log(command, interceptionContext);
    }

    public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        Log(command, interceptionContext);
    }

    private static void Log<T>(DbCommand command, DbCommandInterceptionContext<T> interceptionContext)
    {
        DateTime startTime;
        TimeSpan duration;


        if (m_StartTime.TryRemove(command, out startTime))
        {
            duration = DateTime.Now - startTime;
        }
        else
            duration = TimeSpan.Zero;

        var requestId =-1;
        string message;

        var parameters = new StringBuilder();
        foreach (DbParameter param in command.Parameters)
        {
            parameters.AppendLine(param.ParameterName + " " + param.DbType + " = " + param.Value);
        }

        if (interceptionContext.Exception == null)
        {
            message = string.Format("Database call took {0} sec. RequestId {1} \r\nCommand:\r\n{2}", duration.TotalSeconds.ToString("N3"), requestId, parameters.ToString() + command.CommandText);
        }
        else
        {
            message = string.Format("EF Database call failed after {0} sec. RequestId {1} \r\nCommand:\r\n{2}\r\nError:{3} ", duration.TotalSeconds.ToString("N3"), requestId, parameters.ToString() + command.CommandText, interceptionContext.Exception);
        }

        Debug.WriteLine(message);
    }


    public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        OnStart(command);
    }

    public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
    {
        OnStart(command);
    }

    public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        OnStart(command);
    }
    private static void OnStart(DbCommand command)
    {
        m_StartTime.TryAdd(command, DateTime.Now);
    }
}

不幸的是,文档不存在,所以我不知道这是否适用于您的场景。

关于c# - Entity Framework 6 - 计时查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27196944/

相关文章:

sql-server - 微软 azure : Could not submit the request to create database 'Name'

sql - 交叉联接行为 (SQLServer 2008)

asp.net-mvc - 单独项目中的 VS2010 MVC 和 Entity Framework 模型

c# - 重用部分 linq 查询

c# - PetaPoco + 工作单元 + 存储库模式

c# - 企业应用本地化

c# - Entity Framework - 缺少列?

c# - 将项目添加到List <>的其他方法

sql-server - Visual Studio 2015 中的 SQL Server 数据工具

entity-framework - 使用访问 token 连接到 SQL Azure DB 的 Entity Framework