实体框架Db拦截器为查询中的每个项创建一个日志

时间:2017-07-21 11:47:58

标签: entity-framework logging entity-framework-6 interceptor

我开始使用Dbinterceptor和Entity Framework来跟踪SQL查询。它工作得很完美,除了它为查询中的每个项目记录。例如:如果我执行这样的查询:

context.People.Where(p=>p.Age > 20).ToList()

如果查询返回50个项目,则会将sql查询记录50次。 在我的情况下,我只需要一次。我怎样才能做到这一点?

这是我的拦截器:

internal class EFInterceptor : IDbCommandInterceptor
{
    public EFInterceptor(IDataProviderInternal dataProvider)
    {
        this.dataProvider = dataProvider;
    }

    IDataProviderInternal dataProvider;
    Guid token = Guid.NewGuid();
    private void Query(string message, bool startLog)
    {
        if (startLog && !dataProvider.QueryStarted)
        {
            dataProvider.QueryStarted = true;
            token = Guid.NewGuid();
            EagleFrameworkEventSource.Log.QueryStarted(token, message);
        }
        else if(!startLog && dataProvider.QueryStarted)
        {
            dataProvider.QueryStarted = false;
            EagleFrameworkEventSource.Log.QueryFinished(token);
        }
    }

    public void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {

        //LogInfo("NonQueryExecuted", String.Format(" IsAsync: {0}, Command Text: {1}, Parameters: {2}", interceptionContext.IsAsync, command.CommandText, string.Join(",", command.Parameters)));
        Query(String.Format(" IsAsync: {0}, Command Text: {1}, Parameters: {2}", interceptionContext.IsAsync, command.CommandText, string.Join(",", command.Parameters)), false);
    }

    public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        //LogInfo("NonQueryExecuting", String.Format(" IsAsync: {0}, Command Text: {1}, Parameters:{2}", interceptionContext.IsAsync, command.CommandText, string.Join(",", command.Parameters)));
        Query(String.Format(" IsAsync: {0}, Command Text: {1}, Parameters:{2}", interceptionContext.IsAsync, command.CommandText, string.Join(",", command.Parameters)),true);
    }

    public void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
    {
        //LogInfo("ReaderExecuted", String.Format(" IsAsync: {0}, Command Text: {1}, Parameters:{2}", interceptionContext.IsAsync, command.CommandText, string.Join(",", command.Parameters)));
        Query(String.Format(" IsAsync: {0}, Command Text: {1}, Parameters:{2}", interceptionContext.IsAsync, command.CommandText, string.Join(",", command.Parameters)),false);
    }

    public void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
    {
        //LogInfo("ReaderExecuting", String.Format(" IsAsync: {0}, Command Text: {1}, Parameters:{2}", interceptionContext.IsAsync, command.CommandText, string.Join(",", command.Parameters)));
        Query(String.Format(" IsAsync: {0}, Command Text: {1}, Parameters:{2}", interceptionContext.IsAsync, command.CommandText, string.Join(",", command.Parameters)),true);
    }

    public void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        //LogInfo("ScalarExecuted", String.Format(" IsAsync: {0}, Command Text: {1}, Parameters:{2}", interceptionContext.IsAsync, command.CommandText, string.Join(",", command.Parameters)));
        Query(String.Format(" IsAsync: {0}, Command Text: {1}, Parameters:{2}", interceptionContext.IsAsync, command.CommandText, string.Join(",", command.Parameters)),false);
    }

    public void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        //LogInfo("ScalarExecuting", String.Format(" IsAsync: {0}, Command Text: {1}, Parameters:{2}", interceptionContext.IsAsync, command.CommandText, string.Join(",", command.Parameters)));
        Query(String.Format(" IsAsync: {0}, Command Text: {1}, Parameters:{2}", interceptionContext.IsAsync, command.CommandText, string.Join(",", command.Parameters)), true);
    }

    private void LogInfo(string command, string commandText)
    {
        Console.WriteLine("Intercepted on: {0} :- {1} ", command, commandText);
    }
}

当frist查询开始时,它会在完成第一个查询之前启动下一个查询。因此我添加了一个代码以避免多次日志记录,但似乎它正在为查询中的每个项创建一个新的dataProvider实例,这是不可能的。它看起来非常时髦。谁知道发生了什么?

0 个答案:

没有答案