Skip to content

RelationalCommandDiagnosticsLogger - how to override log level? #36936

@patrickpracht

Description

@patrickpracht

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

Stack traces


Verbose output


EF Core version

9.0.8

Database provider

Microsoft.EntityFrameworkCore.SqlServer

Target framework

.NET 8.0

Operating system

No response

IDE

No response

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions