将 Serilog Entity Framework 命令文本格式化为 seq

Format Serilog Entity Framework command text to seq

我通过 Serilog 将实体查询保存到 seq 服务器。这是我的 Serilog 配置:

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;

这是我的 EF Core 配置:

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

这是序列中的结果:

但我想要这样的结果:

如您所见,我希望在事件序列中 commandText 属性。

改变q.LogTo(Log.Logger.Information, Microsoft.Extensions.Logging.LogLevel.Information);

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"));

您可以使用 RelationalEventId class

过滤您的日志