Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog and ExpandoObject

If I have ExpandoObject like this:

dynamic d = new ExpandoObject();
d.x = "a";
d.y = "b";

and log it with Serilog to RollingFile using JsonFormatter like this:

_logger.Debug("{@d}", d);

it will be serialised to json like this:

[{"_typeTag":"KeyValuePair`2","Key":"x","Value":"a"},{"_typeTag":"KeyValuePair`2","Key":"y","Value":"b"}]

If I use Newtonsoft.Json to serialise the same ExpandoObject like this:

JsonConvert.SerializeObject(d)

I will get this:

{"x":"a","y":"b"}

How can I make Serilog produce the same json as Newtonsoft.Json does?

like image 275
Andrew Avatar asked May 09 '26 02:05

Andrew


1 Answers

Adding:

.Destructure.ByTransforming<ExpandoObject>(e => new Dictionary<string,object>(e))

to your LoggerConfiguration should do it.

like image 132
Nicholas Blumhardt Avatar answered May 12 '26 11:05

Nicholas Blumhardt