本文介绍了我可以在 EntityFramework Core 中配置拦截器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在旧的(pre .net core)时代的实体框架6如下图所示博客文章 有一种方法可以配置拦截器,它可以记录所有慢查询,包括堆栈回溯.

In the old (pre .net core) era's entity framework 6 as shown in this blog post there is a way to configure an interceptor which can log all slow queries including a stack backtrace.

[ 注意:在 3.0 版之前的 Entity Framework Core 中,这是不可能的,因此最初的问题询问该怎么做.自从提出这个问题以来,EF Core 的新选项和新版本已经发布.这个问题现在本质上是历史问题,后来添加的一些答案参考了其他较新版本的 EF Core,其中可能重新引入了拦截器,以实现与 pre-core 时代实体框架的功能对等]

[ NOTE: In Entity Framework Core prior to version 3.0 this was not possible, thus the original question asked what to do instead. Since the time this question was asked, new options and new versions of EF Core have been released. This question is historical now in nature, and some of the answers that were added later reference other newer versions of EF Core, where interceptors may have been reintroduced, to achieve feature parity with the pre-core era entity framework ]

2015 年的一个关于当时称为 EF7 的早期测试版的问题表明它不是 可能在 asp.net vnext 早期测试版中.

A question from 2015 about an earlier beta of what was then called EF7, suggests that it was not possible yet in asp.net vnext early betas.

然而,EF Core 的整个设计是可组合的,并且在 github 错误跟踪器的讨论中 在这里 一种技术可能是可能的,你可以将一些低级类子类化,例如 SqlServerConnection 然后覆盖那里的一些方法,以获得一些可以在执行查询之前和之后挂钩的点,如果执行了毫秒计时器值,则添加一些低级别日志记录.

Yet, the whole design of EF Core is to be composable, and in discussions on github bug tracker here that a technique might be possible where you subclass some low level class like SqlServerConnection and then override some method in there, to get some points you could hook before and after a query is executed, and add some low level logging if a millisecond timer value was executed.

(2020 年删除了对 2015 年预发布信息的引用)

( References to pre-release information from 2015 removed in 2020)

推荐答案

更新:拦截数据库操作现在在 EF Core 3.0 中可用.

Update: Interception of database operations is now available in EF Core 3.0.

原答案:

EF Core 还没有拦截器"或类似的生命周期钩子.此功能在此处跟踪:https://github.com/aspnet/EntityFramework/issues/626.

EF Core does not have "interceptors" or similar lifecycle hooks yet. This feature is tracked here: https://github.com/aspnet/EntityFramework/issues/626.

如果您只想输出日志,则可能没有必要覆盖低级组件.许多低级 EF Core 组件已经生成日志记录,包括查询执行的日志记录.您可以通过调用 DbContextOptionsBuilder.UseLoggerFactory(ILoggerFactory factory) 将 EF 配置为使用自定义记录器工厂.(参见 https://docs.asp.net/en/latest/fundamentals/logging.htmlhttps://github.com/aspnet/Logging 了解更多详情记录器接口.)EF Core 生成一些具有明确定义的事件 ID 的显着日志事件.(请参阅 1.0.0-rc2 中的 Microsoft.EntityFrameworkCore.Infrastructure.CoreLoggingEventId,对于 1.0.0 RTM,该代码已重命名为 Microsoft.EntityFrameworkCore.Infrastructure.CoreEventId.)请参阅https://docs.efproject.net/en/latest/miscellaneous/logging.html 有关这样做的示例.

Overriding a low-level component may be unnecessary if all you want is log output. Many low-level EF Core components already produce logging, logging including query execution. You can configure EF to use a custom logger factory by calling DbContextOptionsBuilder.UseLoggerFactory(ILoggerFactory factory). (See https://docs.asp.net/en/latest/fundamentals/logging.html and https://github.com/aspnet/Logging for more details on this logger interface.) EF Core produces some notable log events with well-define event IDs. (See Microsoft.EntityFrameworkCore.Infrastructure.CoreLoggingEventId in 1.0.0-rc2, which was renamed to justMicrosoft.EntityFrameworkCore.Infrastructure.CoreEventId for 1.0.0 RTM.) See https://docs.efproject.net/en/latest/miscellaneous/logging.html for examples of doing this.

如果您需要除 EF Core 组件已经生成的内容之外的其他日志记录,您将需要覆盖 EF Core 的较低级别组件.这最好通过覆盖现有组件并通过依赖注入将此覆盖版本添加到 EF 来完成.这样做需要为 EF 配置一个自定义服务提供程序以供内部使用.这是由 DbContextOptionsBuilder.UseInternalServiceProvider(IServiceProvider services) 配置的,参见 https://docs.efproject.net/en/latest/miscellaneous/internals/services.html 有关 EF 如何在内部使用服务的更多详细信息.

If you need additional logging beyond what EF Core components already produce, you will need to override EF Core's lower-level components. This is best done by overriding the existing component and added this overridding version to EF via dependency injection. Doing this requires configuring a custom service provider for EF to use internally. This is configured by DbContextOptionsBuilder.UseInternalServiceProvider(IServiceProvider services) See https://docs.efproject.net/en/latest/miscellaneous/internals/services.html for more details on how EF uses services internally.

这篇关于我可以在 EntityFramework Core 中配置拦截器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 19:12