This is a fork of the original Redbox project, updated for Serilog v3 and with a .NET 8.0 example.
A Stackdriver JSON Formatter for logging with Serilog and .NET. Useful when a dependency on the Google SDK is not wanted or when a logs are being sent to Stackdriver using a data collector or log shipper (e.g. Fluentd).
There is no dependency on any particular Serilog Sinks. Pass in an instance of the StackdriverJsonFormatter
class into the Sink of your choice.
A netstandard2.0
Nuget package is available here.
Or you can install with the dotnet cli:
dotnet add package Raileasy.Serilog.Stackdriver
using Raileasy.Serilog.Stackdriver
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console(new StackdriverJsonFormatter()) // Other sinks can be used to, e.g. File
.CreateLogger();
Be sure to add .ReadFrom.Configuration(configuration)
to your Serilog setup first! (Serilog docs)
"Serilog": {
"Using": [
"Serilog.Sinks.Console"
],
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": "Raileasy.Serilog.Stackdriver.StackdriverJsonFormatter, Raileasy.Serilog.Stackdriver"
}
}]
}
The class StackdriverJsonFormatter
has two optional arguments:
Default true
. Detects if a long line is longer than the Stackdriver limit and if so adds an additional FATAL log warning of this.
Stackdriver will break the long line into multiple lines, which will break search functionality of the json values.
Default true
. If the Serilog Message Template should be included in the logs, e.g. { ... "MessageTemplate" : "Hello from {name:l}" ... }
Defaults to new JsonValueFormatter(typeTagName: "$type")
. A valid Serilog JSON Formatter.