我可以在EntityFramework Core中配置拦截器吗? [英] Can I configure an interceptor in EntityFramework Core?

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

问题描述

在旧的(.net核心之前)时代的实体框架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,其中可能已重新引入了拦截器,以实现与内核前时代实体框架的功能相等]

[ 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的早期Beta版本,这表明它不是

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 bug跟踪器的讨论中这里,当您将诸如 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年发布前信息的引用已在2020年删除)

( 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 .html https://github.com/aspnet/Logging 以获得更多详细信息记录器界面。)EF Core会生成一些带有明确定义的事件ID的显着日志事件。 (请参阅1.0.0-rc2中的 Microsoft.EntityFrameworkCore.Infrastructure.CoreLoggingEventId ,该名称已重命名为 Microsoft.EntityFrameworkCore.Infrastructure.CoreEventId for 1.0.0 RTM。)请参见 https://docs.efproject .net / zh-CN / 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中配置拦截器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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