Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

284
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda