I tried the following log with Serilog:
this.logger.Debug("Incoming metrics data {ClientId}", new { clientid = 54732 });
Serilog produced this output:
Incoming metrics data "{ clientid = 54732 }"
Serilog being a structured logger I've expected it to produce something like this:
Incoming metrics data {ClientId}, {clientId: 54732}
Am I doing something wrong or am I understanding Serilog/structured-logging wrong?
Two things; first, to serialize a structure like this you need to use the structure-capturing operator @
:
this.logger.Debug("Incoming metrics data {@Client}", new { ClientId = 54732 });
This will capture the individual properties of the object passed in, so the event in the rewritten example will have a Client
property with ClientId
sub-property.
This will give you output like:
Incoming metrics data {"ClientId": 54732}
(Assuming you are using the latest Serilog console sink with the default template; other configurations may not print the embedded data JSON-style.)
The second consideration is that the full structure of the event isn't shown in Serilog's text output - it's the "human-friendly" face of Serilog. If you want to record structure, you might plug in CompactJsonFormatter
like so (again assuming the console sink):
.WriteTo.Console(new CompactJsonFormatter())
This works for files etc., too - details at https://github.com/serilog/serilog-formatting-compact .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.