Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

288
Views
Prevent SQL queries from appearing in my logging

I am using Serilog for logging in my console application. I set it up as follows.

// Build configuration
IConfiguration configuration = new ConfigurationBuilder()
    .SetBasePath(ApplicationInfo.DataPath)
    .AddJsonFile("appsettings.json", false, false)
    .Build();

// Configure Serilog
string logFormat = "[{Timestamp:yyyy-MM-dd hh:mm:ss tt}][{Level:u3}] {Message:lj}{NewLine}{Exception}";
Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .WriteTo.Console(LogEventLevel.Verbose, logFormat)
    .WriteTo.File(ApplicationInfo.GetDataFileName("log"), LogEventLevel.Verbose, logFormat)
    .CreateLogger();

AppHost = Host.CreateDefaultBuilder(args)
    .ConfigureServices((context, services) =>
    {
        services.AddDbContext<TTApplicationDbContext>(options =>
        {
            options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
        });
        services.Configure<EmailSettings>(configuration.GetSection("EmailSettings"));
        services.Configure<SftpSettings>(configuration.GetSection("FtpSettings"));
    })
    .UseSerilog()
    .Build();

And in my appsettings.json file.

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

This is working fine except that the SQL queries are showing up in my log apparently, related to Entity Framework exceptions.

I do not want to change the log level. I want to get logging for these errors. I just don't want the actual SQL queries included. It's not even a matter of security. They are just too verbose and make it hard to find the actual errors.

In my ASP.NET Core applications, I must add the following line to enable SQL queries in logging.

builder.EnableSensitiveDataLogging();

I need the opposite of this for my console application.

Note: Please don't be too quick to mark as duplicate. I found similar questions, but couldn't find any that directly answered what I'm asking.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You may filter the particular namespace that generates SQL commands and override

// Configure Serilog
string logFormat = "[{Timestamp:yyyy-MM-dd hh:mm:ss tt}][{Level:u3}] {Message:lj}{NewLine}{Exception}";
Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .WriteTo.Console(LogEventLevel.Verbose, logFormat)
    .WriteTo.File(ApplicationInfo.GetDataFileName("log"), LogEventLevel.Verbose, logFormat)
    .MinimumLevel.Verbose().MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", Serilog.Events.LogEventLevel.Warning)
        .MinimumLevel.Verbose().MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Query", Serilog.Events.LogEventLevel.Warning)
                .CreateLogger();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!