1

I'm using Serilog.Sinks.File and all of my SQL queries are getting logged.

Here is my configuration code:

// Configure Serilog
Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(context.Configuration)
    .WriteTo.File(Path.Combine(applicationContext.GetWorkingPath(), "TrackTrace.WorkerServices.log"),
        rollOnFileSizeLimit: true)
    .CreateLogger();

And here is my appsettings.json.

"Logging": {
  "LogLevel": {
    "Default": "Information",
    "Hangfire": "Information",
    "Microsoft": "Warning",
    "Microsoft.Hosting.Lifetime": "Information"
}

With the default Microsoft prefix set to Warning, I was expecting that to eliminate all of the SQL queries. What am I missing?

Update

Here's how I'm configuring my database.

// Configure database
services.AddDbContext<TrackTraceDbContext>(options =>
{
    options.UseSqlServer(
        connectionString,
        builder => builder.MigrationsAssembly(nameof(TTRailtraxEntities)));
});
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • And how is the DbContext is registered? Can you provide a [mre] to play with? – Guru Stron Aug 29 '23 at 18:59
  • @GuruStron: I've added my code to configure the database. This project has a lot going on so I'm not sure how I'd extract just the necessary parts. I like seeing the SQL queries when debugging. But I would think those are coming from Microsoft and are level Information. So I don't expect to see them here. – Jonathan Wood Aug 29 '23 at 19:09

1 Answers1

1

AFAIK Serilog does not use the default configuration section, try moving the setup to Serilog -> MinimumLevel -> Override:

  "Serilog": {
    "MinimumLevel": {
        "Default": "Information",
        "Override": {
            "Microsoft.EntityFrameworkCore": "Warning"
        }
    },

See the MinimumLevel, LevelSwitches, overrides and dynamic reload of the docs.

Alternatively you can ignore/change log levels on the context itself on per-event type basis (like here):

builder.Services.AddDbContext<TrackTraceDbContext>(b => b.UseSqlServer(...)
    .ConfigureWarnings(w => w.Ignore(RelationalEventId.CommandExecuted)));

Or override the logger factory:

builder.Services.AddDbContext<TrackTraceDbContext>(b => b.UseSqlServer(...)
    .UseLoggerFactory(new NullLoggerFactory()));
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Good point. I'll try that. – Jonathan Wood Aug 29 '23 at 19:16
  • I was just looking for these log entries... do they look like this in event viewer?: "I/O was resumed on database master. No user action is required." (sry to add in a question, just curious and wondering whether to remove this logging myself...) – pcalkins Aug 30 '23 at 18:09
  • @pcalkins not sure TBH. Based on quick googling this is some SQL Server specific message, not sure that EF Core even has access/outputs it. Do you have EF Core/ASP.NET Core logging setup to write to event viewer? – Guru Stron Aug 30 '23 at 18:13
  • ok, thanks... I just have defaults set... I'll start a new question though. – pcalkins Aug 30 '23 at 18:59