在 Google Kubernetes Engine 上設定

您可以使用下列其中一種方式,從 Google Kubernetes Engine 應用程式傳送錯誤至 Error Reporting:

使用 Logging 回報錯誤

GKE 的預設記錄代理程式提供代管解決方案,可部署及管理將叢集記錄傳送至 Cloud Logging 的代理程式。代理程式結構會因叢集版本而異。如要瞭解這個代理程式,請參閱「管理 GKE 記錄」。

Error Reporting 要求例外狀況或堆疊追蹤必須包含在單一記錄項目中。大多數記錄代理程式都能辨識多個記錄列 (每個堆疊框架都會列印在新行上) 代表堆疊追蹤,並將其傳送至 Cloud Logging 做為單一記錄項目。如果代理程式無法將多行重建為單一錯誤,請使用 projects.events.report API 端點,這樣您就能控制錯誤的內容。

使用 Error Reporting API 寫入錯誤

Error Reporting API 提供 report 端點,可將錯誤資訊寫入服務。

  1. Enable the Error Reporting API.

    Enable the API

  2. 使用 REST API 或用戶端程式庫將錯誤回報至 API。

範例

ASP.NET

ASP.NET NuGet 套件會將 ASP.NET 網頁應用程式中未捕捉到的例外狀況回報給 Error Reporting。

安裝 NuGet 套件

如要在 Visual Studio 中安裝 Stackdriver ASP.NET NuGet 套件:

  1. 以滑鼠右鍵按一下您的解決方案,然後選取 [管理解決方案的 NuGet 套件]
  2. 選取 [Include prerelease] (包括搶鮮版) 核取方塊。
  3. 搜尋並安裝名為 Google.Cloud.Diagnostics.AspNet 的套件。

用量

Stackdriver ASP.NET NuGet 套件安裝完成後,請新增下列陳述式至應用程式程式庫,以便開始傳送錯誤至 Stackdriver:

using Google.Cloud.Diagnostics.AspNet;

在您的 .NET 網頁應用程式的 Register 方法中加入下列 HttpConfiguration 程式碼 (請使用實際的專案 ID 取代 your-project-id),以啟用例外狀況的回報功能:

public static void Register(HttpConfiguration config)
{
    string projectId = "YOUR-PROJECT-ID";
    string serviceName = "NAME-OF-YOUR-SERVICE";
    string version = "VERSION-OF-YOUR-SERVCICE";
    // ...
    // Add a catch all for the uncaught exceptions.
    config.Services.Add(typeof(IExceptionLogger),
        ErrorReportingExceptionLogger.Create(projectId, serviceName, version));
    // ...
}

將這個方法新增至 ASP.NET 應用程式後,您就可以在 Google Cloud 主控台的「Error Reporting」部分,查看發生時回報給 Google Cloud的所有未偵測到的例外狀況。

C#

您可以在 GoogleCloudPlatform/dotnet-docs-samples 存放區中找到下列範例。如要使用這項功能,請在建構專案後指定專案 ID

C:\...\bin\Debug> set GOOGLE_PROJECT_ID=[YOUR_PROJECT_ID]

請務必將 [YOUR_PROJECT_ID] 替換為Google Cloud 主控台中的正確值。

然後,使用與下列相似的程式碼傳送例外狀況資料:

public class ErrorReportingSample
{
    public static void Main(string[] args)
    {
        try
        {
            throw new Exception("Generic exception for testing Stackdriver Error Reporting");
        }
        catch (Exception e)
        {
            report(e);
            Console.WriteLine("Stackdriver Error Report Sent");
        }
    }

    /// <summary>
    /// Create the Error Reporting service (<seealso cref="ClouderrorreportingService"/>)
    /// with the Application Default Credentials and the proper scopes.
    /// See: https://developers.google.com/identity/protocols/application-default-credentials.
    /// </summary>
    private static ClouderrorreportingService CreateErrorReportingClient()
    {
        // Get the Application Default Credentials.
        GoogleCredential credential = GoogleCredential.GetApplicationDefaultAsync().Result;

        // Add the needed scope to the credentials.
        credential.CreateScoped(ClouderrorreportingService.Scope.CloudPlatform);

        // Create the Error Reporting Service.
        ClouderrorreportingService service = new ClouderrorreportingService(new BaseClientService.Initializer
        {
            HttpClientInitializer = credential,
        });
        return service;
    }

    /// <summary>
    /// Creates a <seealso cref="ReportRequest"/> from a given exception.
    /// </summary>
    private static ReportRequest CreateReportRequest(Exception e)
    {
        // Create the service.
        ClouderrorreportingService service = CreateErrorReportingClient();

        // Get the project ID from the environement variables.
        string projectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");

        // Format the project id to the format Error Reporting expects. See:
        // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/report
        string formattedProjectId = string.Format("projects/{0}", projectId);

        // Add a service context to the report.  For more details see:
        // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events#ServiceContext
        ServiceContext serviceContext = new ServiceContext()
        {
            Service = "myapp",
            Version = "8c1917a9eca3475b5a3686d1d44b52908463b989",
        };
        ReportedErrorEvent errorEvent = new ReportedErrorEvent()
        {
            Message = e.ToString(),
            ServiceContext = serviceContext,
        };
        return new ReportRequest(service, errorEvent, formattedProjectId);
    }

    /// <summary>
    /// Report an exception to the Error Reporting service.
    /// </summary>
    private static void report(Exception e)
    {
        // Create the report and execute the request.
        ReportRequest request = CreateReportRequest(e);
        request.Execute();
    }
}

Go

請參閱「為 Go 設定 Error Reporting」。

Java

請參閱「設定 Java 適用的 Error Reporting」。

Node.js

請參閱「為 Node.js 設定錯誤回報」。

Ruby

請參閱「為 Ruby 設定 Error Reporting」。

Python

請參閱「設定 Python 適用的 Error Reporting」。

PHP

請參閱「設定 PHP 適用的 Error Reporting」。

查看錯誤群組

前往 Google Cloud 控制台的「Error Reporting」頁面:

前往「錯誤回報

您也可以透過搜尋列找到這個頁面。