使用 EF6 拦截数据库的 CRUD 操作并过滤日志

Intercepting CRUD operations on database using EF6 and filtering logs

我正在尝试在我的 Windows 表单应用程序中实现日志记录,并且我有这段代码可以让我在使用 Entity Framework 6:

时拦截 CRUD 操作
 class EFCommandInterceptor : IDbCommandInterceptor
    {
        public void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
            LogInfo("NonQueryExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
        }

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

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

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

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

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

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

然后我这样添加拦截器:

public class FE6CodeConfig : DbConfiguration
{
    public FE6CodeConfig()
    {
        this.AddInterceptor(new EFCommandInterceptor());
    }
}

现在这一切都很好并且可以工作,我的意思是这是一个不错的小功能...但是,我只想在用户插入或删除记录时登录到我的数据库。

所以我需要command name(插入或删除),table namerow id ,以及 table 中的另一个字段...

现在我看到的是我在这些方法中有 DBCommand。有一个名为 Command Text 的 属性...它的输出如下:

Intercepted on: ReaderExecuting :- IsAsync: False, Command Text: INSERT [dbo].[Student]([FirstName], [StandardId], [LastName])
VALUES (@0, NULL, NULL)
SELECT [StudentID], [RowVersion] FROM [dbo].[Student]
WHERE @@ROWCOUNT > 0 AND [StudentID] = scope_identity()
Intercepted on: ReaderExecuted :- IsAsync: False, Command Text: INSERT [dbo].[Student]([FirstName], [StandardId], [LastName])
VALUES (@0, NULL, NULL)
SELECT [StudentID], [RowVersion] FROM [dbo].[Student]
WHERE @@ROWCOUNT > 0 AND [StudentID] = scope_identity()

我的意思是可能会解析上述字符串中的所有内容...但是有没有更方便的方法来获取这些数据?

仅使用 EF 的强大功能(未测试代码,但希望您能理解):

public void MyContext : DbContext
{
   public override int SaveChanges() //not relevant if it is new, you can do it in another method.
   {
       foreach(var e in this.ChangeTracker.Entries())
       {
           if(e.State == EntityState.Added)
           {
              //log here
           }
       }
       return base.SaveChanges();
   }
}

对于原始查询,您需要解析器。

作为替代拦截命令执行的选项,您可以登录业务逻辑层:

public class ProductBusiness
{
    ILogger logger;

    //...
    public void Create(Product p)
    {
        try
        {
            using (var db = new MyDbContext())
            {
                db.Products.Add(p);
                db.SaveChanges();
                logger.Log($"Create Product - {DateTime.Now} - Id:{p.Id}, Name:{p.Name}");
            }
        }
        catch (Exception ex)
        {
            logger.Log($"Error - {DateTime.Now} - {ex.ToString()}");
            throw;
        }
    }
    //...
}