Google.Cloud.Logging.Console

Google.Cloud.Logging.Console is a package providing acustomconsoleformatterdesigned to be used withCloud Logging.

Installation

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

Registering the formatter

After installing the NuGet package, the formatter can be installedusing theAddGoogleCloudConsole 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 inthe extension method call. For example, to enable scopes to belogged, 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 supportslog correlation andGoogle Cloud Trace log integration via trace IDs.This requires a number of conditions to be met:

  • Your application needs to be exporting log entries to Google Cloud Logging via an agent that reads from stdout.This happens automatically when running on Google Cloud, and is presumably the reason you're using thisconsole formatter.
  • Your application needs to be exporting traces to Google Cloud Trace. This could be performed automaticallyby the runtime, or via explicit code or instrumentation. Google Cloud runtimes export some traces automatically.(For example, Cloud Run willexport traces for received requestsby default, but you would need to export traces for any additional information.)Please refer to the documentation for the runtime you're using for more details.
  • When writing a server application, the current .NETActivityneeds to be initialized with the trace context included in the current request. ASP.NET Core does this for youautomatically based on HTTP headers. Alternatively, if your application is starting traces itself (for example,if it's responding to Pub/Sub messages instead of HTTP requests), it needsto initialize the current Activity explicitly, as described in theDistributed Tracing guide.
  • The console formatter needs to be configured with the Google Cloud Project IDwhere the traces are beeing exported to. When running in Google Cloud, that's usually the same project in which thecode is running.

Configuring the formatter involves specifying theTraceGoogleCloudProjectId properties in theGoogleCloudConsoleFormatterOptions 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 CloudLogging. In particular, it uses the following JSON property names:

  • message: the formatted log message
  • severity: the log entry severity
  • category: the logger category
  • exception: the exception stack trace, if any
  • format_parameters: the format parameters for the message, if any
  • scopes: the scope information for the log entry, if theIncludeScopes option is enabled and there is scope informationavailable

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

Most log entry severity mappings are obvious, but the ASP.NET CoreLogLevel.Trace level maps toDEBUG in Cloud Logging.

When viewing the log entries in the Logs Explorer, the severity isshown in the root of the entry. Other aspects are within thejsonPayload property.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-11-06 UTC.