【发布时间】:2019-05-10 01:35:45
【问题描述】:
我正在尝试在我的 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());
}
}
现在这一切都很好并且有效,我的意思是这是一个不错的小功能......但是,我只想在用户插入或删除记录时登录到我的数据库。
所以我需要 命令名(插入或删除)、表名、行 ID,以及该表中的另一个字段。 .
现在我看到的是这些方法中有 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 实体,例如 .Add()、.Remove() 等 - 您可以在 SaveChanges 执行之前扫描实体在 DbContext 中的状态。
-
如果命令文本对您来说不重要,您可以选择登录业务逻辑层的方法。
-
@eocron 是的,我正在处理实体......所以你的意思是完全跳过使用拦截器......嗯 :) 我可以试试......我的意思是,命令文本对我来说没问题如果它有真正的价值,而不是占位符......
标签: c# winforms logging entity-framework-6 interceptor