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 Console Cloud Logging Logs Viewer.

The request and application logs for your app are collected by a Cloud Logging agent. See Quotas and limits 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.

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.

ASP.NET Core applications write very little to stdout and stderr in a production environment, and Cloud Logging does not parse the log into structured data.

To set up your application to send logs to Cloud 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.
    1. 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.
    2. 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.
                  loggerFactory.AddGoogle(app.ApplicationServices, Configuration["Stackdriver:ProjectId"]);
                  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 application logs

You can view your application logs using the Logs Viewer:

  1. Go to the Logs Viewer in the Cloud Console:

    Go to the Logs Viewer

  2. Select an existing Google Cloud project at the top of the page.

  3. From the drop-down menus, select GAE Application as your resource type to see your App Engine logs.

You can use the drop-down menus and other components of the Logs Viewer to filter or search the logs for relevant entries. See Logs Viewer filter interfaces for more details.

Filtering application logs

To filter log entries by label or text search in the Logs Viewer, see Viewing logs.

To view the logs for specific instances, services, or versions, filter by that resource's ID in the Logs Viewer. To find service and version IDs look in the App Engine dashboard.

In the Logs Viewer, log entries correlated by the same trace can be viewed in a "parent-child" format.

Following are instructions for correlating log entries:

  1. Decide which app logs you want to correlate with your request logs. Note that, for this correlation to work, you must choose app logs with a type that is not the same as the request log type.

  2. In your application code, look for the X-Cloud-Trace-Context HTTP header of incoming requests. Extract the trace identifier from the header. For details, see Cloud Trace support.

  3. Set the trace identifier in the LogEntry trace field of your app log entries. The expected format is projects/[PROJECT_ID]/traces/[TRACE_ID].

Once you have correlated the log entries, following are instructions for viewing correlated log entries:

  1. Open your App Engine logs in the Logs Viewer; for details, see Viewing application logs.

  2. Expand a request log by clicking the small black triangle to the left of the log entry. You see your request and app logs displayed in a nested format.


  • The highest severity from the "child" log entries does not automatically apply to the top-level entry. If that behavior is desired, manually set the highest severity in the top-level entry.
  • Set the httpRequest.requestUrl field for the top-level log.

Reading logs via API

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