在 Serilog 中使用 EFCore 日志记录时如何限制 commandText 大小? (和 Serilog MS 日志扩展。)
Program.cs
int commandTextMaxLength = 300;
IHostBuilder builder = Host.CreateDefaultBuilder(args)
.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Destructure.ByTransforming<CommandEventData>(
logData => logData.Command?.CommandText?.Length > commandTextMaxLength ?
new {
//logData.Connection,
Command = new
{
IsTrucated = true,
CommandText = logData.Command.CommandText.Substring(0, commandTextMaxLength) + "...",
logData.Command.CommandTimeout,
logData.Command.CommandType,
logData.Command.Parameters,
//logData.Command.Site
//logData.Command.UpdatedRowSource
},
logData.Context,
logData.ExecuteMethod,
logData.CommandId,
logData.ConnectionId,
logData.IsAsync,
logParameterValues=false,
logData.StartTime,
logData.CommandSource
}
: logData)
// optional .Enrich.FromLogContext()
// optional .Enrich.WithProcessId()
.WriteTo.Console()
.ConfigureLogging(builder => builder.AddSerilog())
;