view raw
Warren  P Warren P - 9 months ago 71
C# Question

Can I configure an interceptor yet in EntityFramework Core 1.0 rc2?

In 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.

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

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

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.

Is that still necessary and the only technique in EF Core 1.0.0-rc2-final (current as of June 2016) ?


EF Core does not have "interceptors" or similar lifecycle hooks yet. This feature is tracked here:

Overriding a low-level component may be unnecessary if all you want is log output. Many low-level EF Core components already produces logging, logging including query execution. You can configure EF to use a custom logger factory by calling DbContextOptionsBuilder.UseLoggerFactory(ILoggerFactory factory). (See and 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 for examples of doing this.

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 exist 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 for more details on how EF uses services internally.