实体框架6 - 定时查询 [英] Entity Framework 6 - Timing queries

查看:107
本文介绍了实体框架6 - 定时查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架6和它的真棒数据库拦截功能来记录哪些正在从应用程序发送的数据库查询。不过,我挣扎着,那些疑问,我有一个长期运行的查询返回的几千几百至几百万行,因此根据此查询将要返回的数据量需要大约6到15秒。实体框架是返回一个SqlDataReader,因为它的,我不能得到它需要得到结果的准确时间。我想知道从时间查询完整的执行时间被送到最后一排被读取时间。有没有一种方法,我可以做到这一点。

I am using Entity Framework 6 and it's awesome database interceptor features to log queries which are being sent from application the database. However, I am struggling to time those queries, I have a long running query which returns hundred of thousands to millions of rows so it takes around 6 to 15 seconds depending on the amount of data this query is going to return. Entity framework is returning a SqlDataReader because of which I cannot get the exact time it takes to get the result. I want to know the complete execution time from the time query was sent to the time last row was read. Is there a way I can do it.

推荐答案

下面是记录我通常使用的EF。

Here is the logger I usually use for 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;

        m_StartTime.TryRemove(command, out startTime);
        if (startTime != default(DateTime))
        {
            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);
    }
}



不幸的是,文件是不存在的,所以我不要知道这是否会为您的方案工作。

Unfortunately the documentation is non-existent so I don't know if this will work for your scenario.

这篇关于实体框架6 - 定时查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆