dotnet / dotnet/efcore

RelationalCommandDiagnosticsLogger - how to override log level?

Open
#36,936 1 comment 0 reactions 0 assignees View on GitHub
area-logging customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

### Question

Hi,

I'm currently trying to achieve this goal
* Log `RelationalEventId.CommandExecuted` (20101) with a log level of `Debug` in the default case
* Log `RelationalEventId.CommandExecuted` (20101) with a log level of `Info` in cases when the execution took longer than a certain threshold

With this approach, we want to declutter our logging by limiting logs to levels of at least `Info`.

I've managed to configure the warning levels during configuration of the context, but that also fixates the internal log level of the event definition to that level.

I couldn't find a way to change the log level on demand, because all possible extension points or properties I've managed to explore are readonly or internal.

The closest I could get was to
* configure my default log level to Info
* create a facade for the internal `RelationalCommandDiagnosticsLogger`
* replace the service for it (`options.ReplaceService<...>()`) when configuring the context
* override the two `CommandReaderExecuted` methods in my facade
* suppress calling `base.CommandReaderExecuted(...)` when `duration` doesn't exceed the threshold

This will, however, prevent the interceptors from being invoked. That's not ideal and achieves only half of my goal.

What part did I miss?

Thank you!

### Your code

```csharp

```

### Stack traces

```text

```

### Verbose output

```text

```

### EF Core version

9.0.8

### Database provider

Microsoft.EntityFrameworkCore.SqlServer

### Target framework

.NET 8.0

### Operating system

_No response_

### IDE

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.