Serilog sink that writes events to Google Cloud Platform (Stackdriver) Logging.
$ dotnet add package Serilog.Sinks.GoogleCloudLoggingSerilog sink that writes events to Google Cloud Logging.
Built for net6.0, net5.0, netstandard2.0
Release notes here: CHANGELOG.md
dotnet add package Serilog.Sinks.GoogleCloudLogging
var config = new GoogleCloudLoggingSinkOptions { ProjectId = "YOUR_PROJECT_ID", UseJsonOutput = true };
Log.Logger = new LoggerConfiguration().WriteTo.GoogleCloudLogging(config).CreateLogger();
This requires the serilog-settings-configuration package.
// appsettings.json or other config file
"Serilog": {
"Using": [ "Serilog.Sinks.GoogleCloudLogging" ],
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "GoogleCloudLogging",
"Args": {
"projectID": "PROJECT-ID-12345",
"labels": {
"foo": "bar"
},
"useSourceContextAsLogName": true,
"useJsonOutput": true,
"restrictedToMinimumLevel": "Information"
}
}
]
}
var config = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(config).CreateLogger();
This library uses the Google Cloud .NET client and Application Default Credentials. The Logs Writer permission is required to send logs.
GoogleCredentialJson config option to a specific service account.| Option | Description |
|---|---|
| ProjectId | ID (not name) of GCP project where logs will be sent. Optional if running in GCP. Required if running elsewhere or to override the destination. |
| ResourceType | Resource type for logs. Optional, automatically identified if running in GCP or will be set to "global". See Monitored Resources and Services for supported types. |
| LogName | Name of the log. Optional, set to "Default". |
| Labels | Optional Dictionary<string, string> labels added to all log entries. |
| ResourceLabels | Optional Dictionary<string, string> labels added to all log entries, for the resource type. See Monitored Resources and Services for recognized labels. |
| ServiceName | Name of the service added as metadata to log entries. |
| ServiceVersion | Version of the service added as metadata to log entries. |
| UseSourceContextAsLogName | The log name for a log entry will be set to the SourceContext property if available. |
| UseJsonOutput | Serialize log entries as JSON for structured logging, see details below. Default is false. |
| UseLogCorrelation | Integrate logs with Cloud Trace by setting Trace, SpanId, TraceSampled properties if available. Default is false. |
| GoogleCredentialJson | Override Application Default Credentials with the content of a JSON credential file. |
TextPayload. Enable UseJsonOutput to send logs as a JsonPayload with rich data types for better querying support and will also capture property names even if they have null values.double so all numbers will be converted. Large integers and floating-point values will lose precision. If you want the exact value preserved then log it as a string instead.This table shows the mapping from Serilog LogLevel to Google Cloud Logging LogSeverity
| Serilog | Cloud Logging |
|---|---|
| Verbose | Debug |
| Debug | Debug |
| Information | Info |
| Warning | Warning |
| Error | Error |
| Fatal | Critical |
View and query logs in the Google Cloud Console Logs Explorer: https://console.cloud.google.com/logs/viewer