Setting up on App Engine Flexible Environment

Stackdriver Error Reporting is automatically enabled for App Engine flexible environment applications. No additional setup is required. Errors written to stderr, which App Engine writes to, are analyzed and displayed by the Error Reporting service.

See Viewing Errors for instructions on viewing error and exception data for your application in the Cloud Platform Console.

Examples using the instrumentation libraries

If you'd like to customize the error information being sent to Error Reporting, you can use the instrumentation libraries available for a number of languages.

C#

Refer to the README for Stackdriver and App Engine Flexible Environment for instructions on running and deploying the Stackdriver sample application.

Google.Cloud.Diagnostics.AspNetCore is a .NET Core client library that enables error reporting for your application using the Logging API v2 and optionally the Error Reporting API v1beta1.

To use the Google.Cloud.Diagnostics.AspNetCore client library:

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

  1. Select the Include prerelease checkbox.

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

  3. Once the Google.Cloud.Diagnostics.AspNetCore client is installed, add the following using directive to your application's source code files when you want to customize the error information being sent to Error Reporting.

     using Google.Cloud.Diagnostics.AspNetCore;
    

Then complete the following 3 steps to log custom error reports from your application:

  1. Add the following code to your web application's appsettings.json file and update YOUR-GOOGLE-PROJECT-ID to be your project ID or number. Also update NAME-OF-YOUR-SERVICE and VERSION-OF-YOUR-SERVICE to appropriate values for your application:

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

  2. 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"));
    services.AddGoogleExceptionLogging(options =>
    {
        options.ProjectId = Configuration["Stackdriver:ProjectId"];
        options.ServiceName = Configuration["Stackdriver:ServiceName"];
        options.Version = Configuration["Stackdriver:Version"];
    });
    

  3. Edit the Configure method in your application's Startup.cs file and add the following code:

    // Configure error reporting service.
    app.UseGoogleExceptionLogging();
    

Now you can send customized error messages as shown in the following example:

throw new Exception("Generic exception for testing Stackdriver Error Reporting");

Go

See Setting Up Stackdriver Error Reporting for Go.

Java

See Setting Up Stackdriver Error Reporting for Java.

Node.js

See Setting Up Stackdriver Error Reporting for Node.js.

PHP

Refer to the README for deployment instructions.

// Imports the Google Cloud client library
use Google\Cloud\Logging\LoggingClient;

// These variables are set by the App Engine environment. To test locally,
// ensure these are set or manually change their values.
$projectId = getenv('GCLOUD_PROJECT') ?: 'YOUR_PROJECT_ID';
$service = getenv('GAE_SERVICE') ?: 'error_reporting_quickstart';
$version = getenv('GAE_VERSION') ?: '1.0-dev';

// Instantiates a client
$logging = new LoggingClient([
    'projectId' => $projectId
]);

// The name of the log to write to
$logName = 'my-log';

// Selects the log to write to
$logger = $logging->logger($logName);

$handlerFunction = function (Exception $e) use ($logger, $service, $version) {
    // Creates the log entry with the exception trace
    $entry = $logger->entry([
        'message' => sprintf('PHP Warning: %s', $e),
        'serviceContext' => [
            'service' => $service,
            'version' => $version,
        ]
    ]);
    // Writes the log entry
    $logger->write($entry);

    print("Exception logged to Stack Driver Error Reporting" . PHP_EOL);
};

// Sets PHP's default exception handler
set_exception_handler($handlerFunction);

throw new Exception('This will be logged to Stack Driver Error Reporting');

Python

See Setting Up Stackdriver Error Reporting for Python.

Ruby

See Setting Up Stackdriver Error Reporting for Ruby.

Monitor your resources on the go

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

Send feedback about...

Stackdriver Error Reporting