Version latestkeyboard_arrow_down

Google.Cloud.Logging.Console

Google.Cloud.Logging.Console is a package providing a custom console formatter designed to be used with Cloud Logging.

Installation

Install the Google.Cloud.Logging.Console package from NuGet. Add it to your project in the normal way (for example by right-clicking on the project in Visual Studio and choosing "Manage NuGet Packages...").

Registering the formatter

After installing the NuGet package, the formatter can be installed using the AddGoogleCloudConsole extension method:

using Google.Cloud.Logging.Console;
...
var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddGoogleCloudConsole();

Additional options can be provided either in appsettings.json or in the extension method call. For example, to enable scopes to be logged, you could change the above code to:

using Google.Cloud.Logging.Console;
...
var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddGoogleCloudConsole(options => options.IncludeScopes = true);

Log trace correlation

The console formatter supports log correlation and Google Cloud Trace log integration via trace IDs. This requires a number of conditions to be met:

Configuring the formatter involves specifying the TraceGoogleCloudProjectId properties in the GoogleCloudConsoleFormatterOptions used to create the formatter, as shown below:

using Google.Cloud.Logging.Console;
...
var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddGoogleCloudConsole(options => options.TraceGoogleCloudProjectId = "google-project-id");

Formatter output

The formatter writes JSON entries in the format expected by Cloud Logging. In particular, it uses the following JSON property names:

No timestamp is included in the JSON, as this is automatically picked up by the logging agent.

Most log entry severity mappings are obvious, but the ASP.NET Core LogLevel.Trace level maps to DEBUG in Cloud Logging.

When viewing the log entries in the Logs Explorer, the severity is shown in the root of the entry. Other aspects are within the jsonPayload property.