efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Disable `Microsoft.Extensions.Logging` logging on a DbContext instance

Open domagojmedo opened this issue 3 years ago • 2 comments

Having queries and execution times in log is awesome feature, but for some queries it's not that useful and can generate a lot of noise. For example when inserting or updating tons of records, log output is not really useful.

It would be nice if we could disable log on a context instance. For example context.DisableLogging() or something like that.

domagojmedo avatar Aug 09 '22 20:08 domagojmedo

@domagojmedo What kind of logging are you using?

ajcvickers avatar Aug 10 '22 09:08 ajcvickers

What do you mean by "kind" of logging? I use Microsoft.Extensions.Logging if that's what you meant

domagojmedo avatar Aug 10 '22 09:08 domagojmedo

There are a few options for handling this by either adjusting the config or rolling your own: https://stackoverflow.com/questions/42079956/suppress-sql-queries-logging-in-entity-framework-core https://docs.microsoft.com/en-us/dotnet/core/extensions/custom-logging-provider

But obviously that's not the same as one line of code at runtime (global or per action).

OpenSpacesAndPlaces avatar Aug 25 '22 15:08 OpenSpacesAndPlaces

Somewhat related I think, I worked around this by tagging queries and filtered based on those (using Serilog).

davidfenko avatar Sep 01 '23 10:09 davidfenko

@davidfenko most issues I have with this is for inserts. I'm don't think you can tag those

domagojmedo avatar Sep 01 '23 10:09 domagojmedo