Search code examples
.netserilog

Serilog remove $type property


I have following logger configuration

new LoggerConfiguration()
  .ReadFrom.Configuration(configuration.GetSection("MyConfig"))
  .WriteTo.Console(new CompactJsonFormatter())
  .Destructure.UsingAttributes()
  .Enrich.WithProperty("CorrelationIdHeader", "someId")
  .CreateLogger()

I'm executing:

logger.Information("{@MyObject}", new MyObject {SomeVal = ""});

Console output json always contains $type property, which I would like to remove

{
  ...
  "$type": "MyObject"
}

How would I remove $type during configuration? Thanks.


Solution

  • You need to construct the CompactJsonFormatter with a JsonValueFormatter that doesn't output the type. You can do that by specifying null as the typeTagName when creating the JsonValueFormatter:

    new CompactJsonFormatter(new JsonValueFormatter(null))