Disable SQL query logging in the Entity Framework core

I have a console .net application using the entity core. The application uses the logging structure to write to the file and the console:

serviceProvider = new ServiceCollection() .AddLogging() .AddDbContext<DataStoreContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))) .BuildServiceProvider(); //configure console logging serviceProvider.GetService<ILoggerFactory>() .AddConsole(LogLevel.Debug) .AddSerilog(); Log.Logger = new LoggerConfiguration() .MinimumLevel.Information() .WriteTo.RollingFile(Path.Combine(Directory.GetCurrentDirectory(), "logs/vcibot-{Date}.txt")) .WriteTo.RollingFile(Path.Combine(Directory.GetCurrentDirectory(), "logs/vcibot-errors-{Date}.txt"), LogEventLevel.Error) .CreateLogger(); logger = serviceProvider.GetService<ILoggerFactory>() .CreateLogger<Program>(); 

The minimum level for file output is set to Information. But with this output setup also contains SQL queries, here is an example:

2017-02-06 10: 31: 38.282 -08: 00 [Information] Done by DbCommand (0ms) [Parameters = [], CommandType = 'Text', CommandTimeout = '30 '] SELECT [f]. [BuildIdentifier], [f]. [Branch], [f]. [BuildDate], [f]. [StaticAssetSizeInKb] FROM [FileSizesHistoryEntries] AS [f]

Is there a way to disable SQL query logging (log them only at the debug log level)

+12
source share
4 answers

If you use the built-in logger, you can add the ILoggingBuilder filter to Program.cs.

So it might look like this:

 WebHost.CreateDefaultBuilder(args) // ... .ConfigureLogging((context, logging) => { var env = context.HostingEnvironment; var config = context.Configuration.GetSection("Logging"); // ... logging.AddConfiguration(config); logging.AddConsole(); // ... logging.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Warning); }) // ... .UseStartup<Startup>() .Build(); 
+14
source

I do not know if this question is relevant, but it is my decision to override the minimum level for "Microsoft.EntityFrameworkCore.Database.Command"

  Log.Logger = new LoggerConfiguration() .MinimumLevel.ControlledBy(loggingLevelSwitch) .MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", Serilog.Events.LogEventLevel.Warning) .Enrich.WithProperty("app", environment.ApplicationName) .Enrich.FromLogContext() .WriteTo.RollingFile($"./Logs/{environment.ApplicationName}") .CreateLogger(); 
+5
source

You want to change the configuration of Serilog to set the minimum level for the Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory context to Warning or higher.

You can find the context that you need to change by setting the output template to something like [{Timestamp:HH:mm:ss} {SourceContext} [{Level}] {Message}{NewLine}{Exception} . Once you know the context, you can return the template back to how it was before.

+4
source

In appsettings.json (or appesttings.Development.json to launch the developer):

 "Logging": { "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Warning" <---- } }, 

Set to Warning instead of Information .

0
source

Source: https://habr.com/ru/post/1263898/


All Articles