A simple JSON formatter for Serilog that uses the System.Text.Json.Utf8JsonWriter
to write the log events to the output stream. As the name suggests, it is entirely built around UTF-8, with all the .NET optimizations for UTF-8, so using other encodings will most likely result in invalid characters. The default for the File sink is UTF-8.
var logger = new LoggerConfiguration()
.WriteTo.File(new Utf8JsonFormatter(), "log.json")
.CreateLogger();
{
"Name": "Console",
"Args": {
"formatter": {
"type": "Serilog.Extensions.Formatting.Utf8JsonFormatter, Serilog.Extensions.Formatting",
// if you want to use a custom naming policy, you can specify it here
"namingPolicy": "System.Text.Json.JsonNamingPolicy::CamelCase, System.Text.Json"
}
}
}
The Utf8JsonFormatter
constructor accepts the following options:
closingDelimiter
: Closing delimiter of the log event. Defaults toEnvironment.NewLine
.renderMessage
: A boolean that determines whether the message template will be rendered. Defaults tofalse
.formatProvider
: AnIFormatProvider
that will be used to format the message template. Defaults toCultureInfo.InvariantCulture
.spanBufferSize
: The size of the buffer used to format theISpanFormattable
values. Defaults to64
.skipValidation
: A boolean that determines whether the JSON writer will skip validation. Defaults totrue
.namingPolicy
: AJsonNamingPolicy
that will be used to convert the property names. Default is leaving the property names as they are.jsonWriterEncoder
: AJavaScriptEncoder
that will be used to encode the JSON output. Defaults tonull
which is considered to be safe. However please note that some Unicode characters will be escaped in the output. More info.
I specifically had a use-case in a project which required logs to be in camelCase
, and none of the built-in formatters supported that, not even ExpressionTemplate
, since I couldn't find a way to specify a custom JsonNamingPolicy
for properties.