Home > database >  Format Serilog Entity Framework command text to seq
Format Serilog Entity Framework command text to seq

Time:01-12

I save entity query to seq server via Serilog. This is my Serilog config:

string seqURL = configuration.GetValue<string>("Logging:Serilog:Seq:url");
string apiKey = configuration.GetValue<string>("Logging:Serilog:Seq:apiKey");
serilogLogger = new LoggerConfiguration()
                          .MinimumLevel.Information()
                          .MinimumLevel
                          .Override("Microsoft.EntityFrameworkCore.Database.Command", Serilog.Events.LogEventLevel.Warning)
                          .Enrich.FromLogContext()
                          .Enrich.WithExceptionDetails()
                          .Enrich.WithProperty("ApplicationName","Portal")
                          .WriteTo.Console()
                          .WriteTo.Seq(seqURL, apiKey:apiKey)
                          .CreateLogger();
Log.Logger = serilogLogger;

And this is my EF Core config :

services.AddDbContext<DBAuth>(q =>
            {
                q.UseSqlServer(Configuration.GetConnectionString("dbAuth"));
                q.LogTo(Log.Logger.Information, Microsoft.Extensions.Logging.LogLevel.Information);
            });

This is the result in seq:

enter image description here

But I want a result like this:

enter image description here

As you can see, I want commandText property in event Seq.

CodePudding user response:

change q.LogTo(Log.Logger.Information, Microsoft.Extensions.Logging.LogLevel.Information); to

q.LogTo((e, l) => e==RelationalEventId.CommandExecuted, a =>
{
    CommandEventData cmdData = (CommandEventData)a;
    Log.Logger
    .Warning("Command Executed : {Command}", cmdData.Command.CommandText);
});
q.UseSqlServer(Configuration.GetConnectionString("db"));

you can filter your log with RelationalEventId class

  •  Tags:  
  • Related