Writing Application Logs

When a request is sent to your application running in App Engine, request and response details are logged automatically, and can be viewed in the Google Cloud Platform Console Stackdriver Logs Viewer.

The request and application logs for your app are collected by a Stackdriver Logging agent. See Quota Policy for the logs retention policy and for the maximum size of log entries. If you want to store your logs for a longer period, you can export your logs to Cloud Storage. You can also export your logs to BigQuery and Pub/Sub for further processing.

For a list of available logs you can use, see App Engine flexible environment logs.

Writing application logs

When your application handles a request, it can write its own logging messages to stdout and stderr. Write your application logs using stdout for output and stderr for errors. These files are automatically collected and can be viewed in the Logs Viewer. Note that this does not provide log levels that you can use for filtering in the Logs Viewer; however, the Logs Viewer does provide other filtering, such as text, timestamp, etc. Only the most recent entries in the Logs Viewer are retained in order to limit their size.

Writing custom logs using the Stackdriver Logging API client library

Google.Cloud.Diagnostics.AspNetCore is a .NET Core client library that includes the Stackdriver Logging API v2 client library. You can use this library to write custom logs.

Installing the client library

To use the Logging client, install the Google.Cloud.Diagnostics.AspNetCore client library:

  1. Right-click your solution in Visual Studio and select Manage Nuget packages for solution.

  2. Select the Include prerelease checkbox.

  3. Search for and install the package named Google.Cloud.Diagnostics.AspNetCore.

Setting up your application

To set up your application to send logs to Stackdriver logging:

  1. Add the following statement to your application code:

    using Google.Cloud.Diagnostics.AspNetCore;
  2. Add the following code to your web application's appsettings.json file:

      "Stackdriver": {
        "ProjectId": "YOUR-GOOGLE-PROJECT-ID",
        "ServiceName": "NAME-OF-YOUR-SERVICE",
        "Version": "VERSION-OF-YOUR-SERVICE"


    • YOUR-GOOGLE-PROJECT-ID is your project ID.
    • NAME-OF-YOUR-SERVICE is your service name.
    • VERSION-OF-YOUR-SERVICE is your service version.
  3. Create or edit the ConfigureServices() method in your application's Startup.cs file and then add the following code:

            public void ConfigureServices(IServiceCollection services)
                services.AddGoogleExceptionLogging(options =>
                    options.ProjectId = Configuration["Stackdriver:ProjectId"];
                    options.ServiceName = Configuration["Stackdriver:ServiceName"];
                    options.Version = Configuration["Stackdriver:Version"];
                // Add trace service.
                services.AddGoogleTrace(options =>
                    options.ProjectId = Configuration["Stackdriver:ProjectId"];
                    options.Options = TraceOptions.Create(
                        bufferOptions: BufferOptions.NoBuffer());
                // Add framework services.

  4. Customize the following code sample and add it to your application's Startup.cs file:

            public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
                // Configure logging service.
                var logger = loggerFactory.CreateLogger("testStackdriverLogging");
                // Write the log entry.
                logger.LogInformation("Stackdriver sample started. This is a log message.");
                // Configure error reporting service.
                // Configure trace service.
                app.UseMvc(routes =>
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");

Viewing logs

To view logs written by apps running in the flexible environment, use the Logs Viewer.

To filter log entries by label or text search in the Logs Viewer interface, see Basic Logs Filters. To write advanced logs filters using expressions specifying a set of log entries from any number of logs, see Advanced Logs Filters.

Tip: To view the logs for specific instances, services, or versions, filter by that resource's ID in the Logs Viewer. On the App Engine dashboard in the GCP Console, service and version IDs are listed in the drop-down menus at the top. Your instance IDs are listed in the GCP Console Instances page.

Go to the App Engine dashboard

Reading logs via API

There is no API for reading App Engine logs created in the flexible environment.

Was this page helpful? Let us know how we did:

Send feedback about...

App Engine flexible environment for .NET docs