dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.82k stars 3.2k forks source link

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

Open domagojmedo opened 2 years ago

domagojmedo commented 2 years ago

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.

ajcvickers commented 2 years ago

@domagojmedo What kind of logging are you using?

domagojmedo commented 2 years ago

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

OpenSpacesAndPlaces commented 2 years ago

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

davidfenko commented 1 year ago

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

domagojmedo commented 1 year ago

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