Install theGoogle.Cloud.Logging.Consolepackage 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 theAddGoogleCloudConsoleextension 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);
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 this
console formatter.
Your application needs to be exporting traces to Google Cloud Trace. This could be performed automatically
by 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 you
automatically 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 needs
to initialize the current Activity explicitly, as described in theDistributed Tracing guide.
The console formatter needs to be configured with the Google Cloud Project ID
where the traces are beeing exported to. When running in Google Cloud, that's usually the same project in which the
code is running.
Configuring the formatter involves specifying theTraceGoogleCloudProjectIdproperties in theGoogleCloudConsoleFormatterOptionsused 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:
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 theIncludeScopesoption is enabled and there is scope information
available
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 CoreLogLevel.Tracelevel maps toDEBUGin 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 thejsonPayloadproperty.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003e\u003ccode\u003eGoogle.Cloud.Logging.Console\u003c/code\u003e is a package that provides a custom console formatter for use with Google Cloud Logging.\u003c/p\u003e\n"],["\u003cp\u003eThe formatter can be installed by adding the \u003ccode\u003eGoogle.Cloud.Logging.Console\u003c/code\u003e NuGet package and then registering it using the \u003ccode\u003eAddGoogleCloudConsole\u003c/code\u003e extension method.\u003c/p\u003e\n"],["\u003cp\u003eThe package supports log correlation and Google Cloud Trace log integration through trace IDs, which requires log entries and traces to be exported to Google Cloud Logging and Trace respectively, and the console formatter to be configured with the project ID.\u003c/p\u003e\n"],["\u003cp\u003eThe formatter outputs log entries in a JSON format compatible with Cloud Logging, including properties such as message, severity, category, exception, format parameters, and scopes.\u003c/p\u003e\n"],["\u003cp\u003eThere are several different versions of the package, with 1.4.0 being the latest version.\u003c/p\u003e\n"]]],[],null,["Version latestkeyboard_arrow_down\n\n- [1.5.0 (latest)](/dotnet/docs/reference/Google.Cloud.Logging.Console/latest)\n- [1.4.0](/dotnet/docs/reference/Google.Cloud.Logging.Console/1.4.0)\n- [1.3.1](/dotnet/docs/reference/Google.Cloud.Logging.Console/1.3.1)\n- [1.2.0](/dotnet/docs/reference/Google.Cloud.Logging.Console/1.2.0)\n- [1.1.0](/dotnet/docs/reference/Google.Cloud.Logging.Console/1.1.0)\n- [1.0.0](/dotnet/docs/reference/Google.Cloud.Logging.Console/1.0.0) \n\nGoogle.Cloud.Logging.Console\n============================\n\n`Google.Cloud.Logging.Console` is a package providing a [custom\nconsole\nformatter](https://docs.microsoft.com/en-us/dotnet/core/extensions/console-log-formatter)\ndesigned to be used with [Cloud Logging](https://cloud.google.com/logging/docs).\n\nInstallation\n------------\n\nInstall the `Google.Cloud.Logging.Console` package from NuGet. Add it to\nyour project in the normal way (for example by right-clicking on the\nproject in Visual Studio and choosing \"Manage NuGet Packages...\").\n\nRegistering the formatter\n-------------------------\n\nAfter installing the NuGet package, the formatter can be installed\nusing the `AddGoogleCloudConsole` extension method: \n\n using Google.Cloud.Logging.Console;\n ...\n var builder = WebApplication.CreateBuilder(args);\n builder.Logging.AddGoogleCloudConsole();\n\nAdditional options can be provided either in appsettings.json or in\nthe extension method call. For example, to enable scopes to be\nlogged, you could change the above code to: \n\n using Google.Cloud.Logging.Console;\n ...\n var builder = WebApplication.CreateBuilder(args);\n builder.Logging.AddGoogleCloudConsole(options =\u003e options.IncludeScopes = true);\n\nLog trace correlation\n---------------------\n\nThe console formatter supports [log correlation](https://cloud.google.com/logging/docs/view/correlate-logs) and\n[Google Cloud Trace log integration](https://cloud.google.com/trace/docs/trace-log-integration) via trace IDs.\nThis requires a number of conditions to be met:\n\n- 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 this console formatter.\n- Your application needs to be exporting traces to Google Cloud Trace. This could be performed automatically by the runtime, or via explicit code or instrumentation. Google Cloud runtimes export some traces automatically. (For example, Cloud Run will [export traces for received requests](https://cloud.google.com/run/docs/trace) by 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.\n- When writing a server application, the current .NET [Activity](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.activity) needs to be initialized with the trace context included in the current request. ASP.NET Core does this for you automatically 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 needs to initialize the current Activity explicitly, as described in the [Distributed Tracing guide](https://learn.microsoft.com/en-us/dotnet/core/diagnostics/distributed-tracing).\n- The console formatter needs to be configured with the Google Cloud Project ID where the traces are beeing exported to. When running in Google Cloud, that's usually the same project in which the code is running.\n\nConfiguring the formatter involves specifying the `TraceGoogleCloudProjectId` properties in the\n`GoogleCloudConsoleFormatterOptions` used to create the formatter, as shown below: \n\n using Google.Cloud.Logging.Console;\n ...\n var builder = WebApplication.CreateBuilder(args);\n builder.Logging.AddGoogleCloudConsole(options =\u003e options.TraceGoogleCloudProjectId = \"google-project-id\");\n\nFormatter output\n----------------\n\nThe formatter writes JSON entries in the format expected by Cloud\nLogging. In particular, it uses the following JSON property names:\n\n- `message`: the formatted log message\n- `severity`: the log entry severity\n- `category`: the logger category\n- `exception`: the exception stack trace, if any\n- `format_parameters`: the format parameters for the message, if any\n- `scopes`: the scope information for the log entry, if the `IncludeScopes` option is enabled and there is scope information available\n\nNo timestamp is included in the JSON, as this is automatically\npicked up by the logging agent.\n\nMost log entry severity mappings are obvious, but the ASP.NET Core\n`LogLevel.Trace` level maps to `DEBUG` in Cloud Logging.\n\nWhen viewing the log entries in the Logs Explorer, the severity is\nshown in the root of the entry. Other aspects are within the\n`jsonPayload` property."]]