Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog remove $type property

Tags:

.net

serilog

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.

like image 436
Pavel Avatar asked Oct 09 '19 11:10

Pavel


1 Answers

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))
like image 192
Martin Liversage Avatar answered Nov 08 '22 21:11

Martin Liversage