Search code examples
c#serializationsequenceserilogsystem.text.json

How does one set JsonSerializerOptions.DefaultIgnoreCondition for Serilog.Sinks.Seq


I'm using Serilog.Sinks.Seq in my C# app (see the configuration code below) but I can't figure out how to set the JsonSerializerOptions.DefaultIgnoreCondition to ignore nulls. Any help in this regard would be greatly appreciated...

using IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((context, services) =>
    {
        services.AddHostedService<Worker>();
    })
    .UseSerilog((context, config) =>
    {
        var seqUri = new Uri(context.Configuration["SeqUri"]!);
        var seqApiKey = context.Configuration["SeqApiKey"]!;

        config.MinimumLevel.Debug()
            .WriteTo.Seq(seqUri.AbsoluteUri, apiKey: seqApiKey);
    })
    .Build();

await host.RunAsync();

Solution

  • WriteTo.Seq does not provide any ways to use JsonSerializerOptions. The Seq server itself is responsible for handling the serialization of the log events. It's designed to work with structured log data so it uses its own serialization format which is built for optimization. I'm not aware of anyways to modify or override it.

    You can use other sinks for logging which provide support for custom serialization Serilog.Sinks.File or Serilog.Sinks.Elasticsearch. You can use these sinks to log and then use Log Shipping to consume those logs in Seq.