13

Is there a way to make Serilog format structured data into a formatted output?

I've been using structured data structures in Serilog lately and even though there is an advantage of it being compact large data structures (5 properties or more) are hard to read in the console/file without formatting it later.

Hypothetically I'd only enable this on dev.

https://github.com/serilog/serilog/wiki/Structured-Data

From this:

{ "Fruit": ["Apple", "Pear", "Orange"] }

To this:

{
  "Fruit": [
    "Apple",
    "Pear",
    "Orange"
  ]
}

Edit: Currently I'm using JsonConvert.SerializeObject({...}, Formatting.Indented) but I'd like to move away from this for reasons like proper coloring from the console package, faster serialization, deferred serialization etc.

jeanfrg
  • 2,366
  • 2
  • 29
  • 40

1 Answers1

3

I seem to recall that we had to do a custom formatter at work a few years ago to modify the default json output from serilog. I do not remember the exact problem we had.

You could take a look at, https://github.com/serilog/serilog/wiki/Formatting-Output, if you haven't already

I have started using https://getseq.net/ to look at the structured output during development, I would highly recommend it.

Christian Sparre
  • 955
  • 3
  • 15
  • 25
  • 1
    I've looked at that. But it specifically refers to the output of the entire logging record. I'd like to achieve the same thing that exceptions do... multi-line formatting for specific data structures. I have a feeling I'll need to write my own formatter implementation. Also, I've update my question to include my current state. – jeanfrg Jul 26 '17 at 13:59
  • 1
    @janfrg I have pretty much the same issue as you - did you ever get anywhere with this? – mutex Aug 09 '21 at 04:35