Skip to content

A Stackdriver JSON Formatter for logging with Serilog and .NET

License

Notifications You must be signed in to change notification settings

raileasyuk/stackdriver-serilog

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stackdriver Formatter for Serilog

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).

Serilog Sinks

There is no dependency on any particular Serilog Sinks. Pass in an instance of the StackdriverJsonFormatter class into the Sink of your choice.

Installing

A netstandard2.0 Nuget package is available here.

Or you can install with the dotnet cli:

dotnet add package Raileasy.Serilog.Stackdriver

Sample Setup Code

Directly into a Serilog Instance

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();

In appsettings.json

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"
        }
    }]
}

Configuration Options

The class StackdriverJsonFormatter has two optional arguments:

checkForPayloadLimit

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.

includeMessageTemplate

Default true. If the Serilog Message Template should be included in the logs, e.g. { ... "MessageTemplate" : "Hello from {name:l}" ... }

valueFormatter

Defaults to new JsonValueFormatter(typeTagName: "$type"). A valid Serilog JSON Formatter.

About

A Stackdriver JSON Formatter for logging with Serilog and .NET

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%