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 Logs Viewer.

When your application handles a request, it can also write its own logging messages to stdout and stderr. These files are automatically collected and can be viewed in the Logs Viewer. Only the most recent entries to stdout and stderr are retained, in order to limit their size.

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 below.

Runtime logs

In addition to App Engine-generated request logs, apps running in the flexible environment can write additional logs. The following are some of the logs that are configured by default:

Log name Purpose
activity Shows audit log information.
nginx.health_check Shows nginx logs specifically for health checks. Reduces noise in the request logs.
nginx.request Shows logs from the nginx proxy as they are passed through to the app.
stdout Standard output from your app.
stderr Standard error from your container.
vm.crash Information logged when setup fails. If your application fails to run, check this log.
vm.events Operational events related to instances.
vm.shutdown Information logged on shutdown.
vm.syslog The VM syslog, outside of the Docker container.

VM events logging

You can gain more insight into events related to your App Engine flexible environment instances using the vm.events stream in Cloud Logging. The following events are logged in this stream:

  • Instance was created
  • Instance was terminated (and why, reported on a best-effort basis)
  • Instance health check aggregate statistics reported approximately every minute
  • Instance unlocked for debugging or Instance was accessed via SSH

These logs might be interleaved, and without general formatting. Your application should not rely on programmatic parsing of these logs.

Shutdown logs

You can obtain diagnostic information when your application's Docker container is stopped. For exmple, this might happen if an app is unresponsive due to a deadlock or high load and has stopped returning requests, including health checks.

To help diagnose issues you can obtain debug information, such as thread traces and heap statistics, from the Docker container through Stackdriver Logging (available in stdout stream).

Writing application logs

Write your application logs using stdout for output and stderr for errors. 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.

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"
      }
    }

    where:

    • 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:

    services.AddOptions();
    services.Configure<StackdriverOptions>(
        Configuration.GetSection("Stackdriver"));
    

  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.
                loggerFactory.AddGoogle(Configuration["Stackdriver:ProjectId"]);
                var logger = loggerFactory.CreateLogger("testStackdriverLogging");
    
                // Write the log entry.
                logger.LogInformation("Stackdriver sample started. This is a log message.");
            }
    

Viewing logs

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

  1. Visit the Logs Viewer page in the Google Cloud Platform Console.

  2. From the log type pulldown menu, pick the log type (stdout, stderr, vm.crash, etc.), that you want to view.

  3. Use the desired filter to retrieve the logs you want to see. You can filter by various combinations of time, log level, module, and log filter label or regular expression.

    Notice that labels are regular expressions for filtering the logs by logging fields. Valid labels include the following:

    • day
    • month
    • year
    • hour
    • minute
    • second
    • tzone
    • remotehost
    • identd_user
    • user
    • status
    • bytes
    • referrer
    • useragent
    • method
    • path
    • querystring
    • protocol
    • request_id

    For example, path:/foo.* useragent:.*Chrome.* gets logs for all requests to a path starting with /foo that were issued from a Chrome browser.

Reading logs via API

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

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

App Engine flexible environment for .NET docs