From b6d76a2f0cf74bbde24e154d3678449dedf10862 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Wed, 18 Sep 2024 21:06:25 -0500 Subject: [PATCH] Suppress logging of EF commands --- LANCommander.Launcher.CLI/Program.cs | 1 + LANCommander.Launcher.Data/DatabaseContext.cs | 7 ++++++- LANCommander.Launcher/Program.cs | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/LANCommander.Launcher.CLI/Program.cs b/LANCommander.Launcher.CLI/Program.cs index 99021a9b..f626ec5d 100644 --- a/LANCommander.Launcher.CLI/Program.cs +++ b/LANCommander.Launcher.CLI/Program.cs @@ -9,6 +9,7 @@ using Serilog; var settings = SettingService.GetSettings(); using var logger = new LoggerConfiguration() + .MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", Serilog.Events.LogEventLevel.Warning) .Enrich.WithProperty("Application", typeof(Program).Assembly.GetName().Name) .WriteTo.Console() .WriteTo.File(Path.Combine(settings.Debug.LoggingPath, "log-.txt"), rollingInterval: settings.Debug.LoggingArchivePeriod) diff --git a/LANCommander.Launcher.Data/DatabaseContext.cs b/LANCommander.Launcher.Data/DatabaseContext.cs index 0ceba1a3..b9670bb9 100644 --- a/LANCommander.Launcher.Data/DatabaseContext.cs +++ b/LANCommander.Launcher.Data/DatabaseContext.cs @@ -1,21 +1,26 @@ using LANCommander.Launcher.Data.Models; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; namespace LANCommander.Launcher.Data { public class DatabaseContext : DbContext { + private readonly ILoggerFactory LoggerFactory; + public DatabaseContext() { } - public DatabaseContext(DbContextOptions options) + public DatabaseContext(ILoggerFactory loggerFactory, DbContextOptions options) : base(options) { + LoggerFactory = loggerFactory; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var dbPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "LANCommander.db"); + optionsBuilder.UseLoggerFactory(LoggerFactory); optionsBuilder.UseSqlite($"Data Source={dbPath};Cache=Shared"); optionsBuilder.UseLazyLoadingProxies(); } diff --git a/LANCommander.Launcher/Program.cs b/LANCommander.Launcher/Program.cs index b0a58e94..04f31ce0 100644 --- a/LANCommander.Launcher/Program.cs +++ b/LANCommander.Launcher/Program.cs @@ -31,6 +31,7 @@ namespace LANCommander.Launcher var settings = SettingService.GetSettings(); using var Logger = new LoggerConfiguration() + .MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", Serilog.Events.LogEventLevel.Warning) .Enrich.WithProperty("Application", typeof(Program).Assembly.GetName().Name) .WriteTo.File(Path.Combine(settings.Debug.LoggingPath, "log-.txt"), rollingInterval: settings.Debug.LoggingArchivePeriod) #if DEBUG