serilog / serilog-settings-configuration

A Serilog configuration provider that reads from Microsoft.Extensions.Configuration
Apache License 2.0
456 stars 129 forks source link

Serilog console logging with JsonFromatter #225

Closed rajurh closed 3 years ago

rajurh commented 4 years ago

I have been trying to use Serilog Console logging with JsonFormatter. When I enable option for console logging in code with .NET core we have option with JsonFromatter to renderMessage= true in its constructor which helps to render message and template in a nicer way and working well.

When I try to define and make it work using appsetting.json configuration for console logging I dont see an option to define "renderMessage" = true due to which when it logs in console I am not getting rendered message.

"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"

As part of configuration setting if you could let me know how to enable render message option will be of great help.

nblumhardt commented 4 years ago

Hi! To do this, install the Serilog.Formatting.Compact package, and use Serilog.Formatting.Compact.RenderedCompactJsonFormatter instead. HTH!

rajurh commented 4 years ago

Hi, I didn't use RenderedCompactJson or CompactJsonFormatter because of following reason Either of them doesn't provide both Message template and Rendered message until you define output message template which I wanted to avoid. What will be your recommendations

nblumhardt commented 4 years ago

Hi! Just grabbing the source for CompactJsonFormatter and adding the @m field using the extra line in RenderedCompactJsonFormatter will do the job - the source is very, very short - it's easier to copy/modify than for us to support a wide variety of flags/options in the formatters. Hope this helps!

btull89 commented 3 years ago

I also would like to be able to use the JsonFormatter and set "renderMessage" = true through appsettings.json.

martinmine commented 1 year ago

For those wondering, this is how you configure this after the 3.3.0 release:

{
  "Name": "File",
  "Args": {
    "path": "log.json",
    "rollingInterval": "Day",
    "retainedFileCountLimit": 7,
    "formatter": {
      "type": "Serilog.Formatting.Json.JsonFormatter, Serilog",
      "renderMessage": true
    }
  }
}