Serilog sink that writes events to Google Cloud Platform Stackdriver Logging.
Install package from Nuget:
Install-Package Serilog.Sinks.GoogleCloudLogging
Configure Logger:
var log = new LoggerConfiguration()
.WriteTo.GoogleCloudLogging(new GoogleCloudLoggingSinkOptions("YOUR_PROJECT_ID"))
.CreateLogger();
This library uses the Google-Cloud-Dotnet
libraries which authenticate using the default service account on the machine. This is automatic on GCE VMs or you can use the gcloud
SDK to authenticate manually. The service account must have the Logs Writer
permission to send logs.
Name | Required | Default | Description |
---|---|---|---|
ProjectId |
Yes | Google Cloud project ID where logs will be sent to. | |
ResourceType |
Yes | global | Resource type for all log output. Must be one of the supported types listed in the cloud logging documentation. |
LogName |
Yes | Default | Name of log under the resource type. |
Labels |
Dictionary of string keys and values to be added to all log entries. | ||
UseSourceContextAsLogName |
True | The log name for a log entry will be set to the SourceContext property if it's available. | |
UseJsonOutput |
False | Structured logging can be sent as text with labels or as a JSON object, details below. |
Serilog uses structured logging so each log statement has a formatting template with attached properties which are then combined to create the final output. When UseJsonOutput
is false, the final output is rendered as the TextPayload
in GCP logs with any properties serialized as string key/value labels.
To maintain the datatypes and structure as much as possible, set UseJsonOutput
to true and the log statement will be serialized and sent as the JsonPayload
instead. This is slightly slower but helpful for querying child properties or numbers in the Log Viewer, and will also capture property names when they have null values.
Logs will appear in the Google Cloud Console Log Viewer: https://console.cloud.google.com/logs/viewer
When using default options, logs will appear under these filter settings: