您可以使用 Google.Cloud.Diagnostics 程式庫集 ,從 C# 應用程式傳送錯誤報告至 Error Reporting。使用 Google.Cloud.Diagnostics 程式庫集 ,為下列情況建立錯誤群組:
- 含有記錄檔項目的記錄檔 bucket 具有客戶自行管理的加密金鑰 (CMEK)。
- 記錄檔 bucket 符合下列其中一項條件:
- 記錄 bucket 會儲存在記錄項目來源的專案中。
- 記錄項目已轉送至專案,然後該專案將這些記錄項目儲存在所屬的記錄 bucket 中。
- 您想回報自訂錯誤事件。
Error Reporting 已與部分 Google Cloud 服務整合,如 Cloud Functions、App Engine、Compute Engine 和 Google Kubernetes Engine。Error Reporting 會顯示在這些服務上執行的應用程式記錄到 Cloud Logging 的錯誤。詳情請參閱本頁面的「在 Google Cloud上執行」一節。
另外,您也可以使用 Logging,將錯誤資料傳送至 Error Reporting。如要瞭解資料格式要求,請參閱「在 Logging 中格式化錯誤訊息」。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Error Reporting API .
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Error Reporting API .
-
如果應用程式的目標是 ASP.NET Core 3 以上版本,請使用
Google.Cloud.Diagnostics.AspNetCore3
程式庫。您可以從 NuGet 安裝這個程式庫。 -
如果應用程式指定較舊版本的 ASP.NET Core,請使用
Google.Cloud.Diagnostics.AspNetCore
程式庫。您可以從 NuGet 安裝這個程式庫。 -
如要以非 ASP.NET Core 架構為目標的應用程式,請使用
Google.Cloud.Diagnostics.Common
程式庫。您可以從 NuGet 安裝這個程式庫。 -
Google.Cloud.Diagnostics.AspNetCore3
說明文件 -
Google.Cloud.Diagnostics.AspNetCore
說明文件 -
Google.Cloud.Diagnostics.Common
說明文件 請確保容器使用的服務帳戶已取得 Error Reporting 寫入者角色 (
roles/errorreporting.writer
)。您可以使用 Compute Engine 預設服務帳戶或自訂服務帳戶。
如要瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」一文。
建立叢集,並授予叢集
cloud-platform
access scope。舉例來說,下列建立指令會指定
cloud-platform
存取權範圍和服務帳戶:gcloud container clusters create CLUSTER_NAME --service-account SERVICE_ACCT_NAME --scopes=cloud-platform
確認 VM 執行個體使用的服務帳戶已取得 Error Reporting 寫入者角色 (
roles/errorreporting.writer
)。您可以使用 Compute Engine 預設服務帳戶或自訂服務帳戶。
如要瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」一文。
-
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面:
如果您是使用搜尋列尋找這個頁面,請選取子標題為「Compute Engine」的結果。
選取要接收
cloud-platform
存取範圍的 VM 執行個體。按一下「停止」,然後按一下「編輯」。
在「Identity and API access」(身分及 API 存取權) 區段,選取具有 Error Reporting 寫入者角色 (
roles/errorreporting.writer
) 的服務帳戶。在「存取權範圍」專區,選取「允許所有 Cloud API 的完整存取權」,然後儲存變更。
按一下「開始/繼續」。
-
Google.Cloud.Diagnostics.AspNetCore3
說明文件 -
Google.Cloud.Diagnostics.AspNetCore
說明文件 -
Google.Cloud.Diagnostics.Common
說明文件
安裝用戶端程式庫
Google.Cloud.Diagnostics 程式庫集 可讓您監控及查看 C# 應用程式所回報的錯誤 (不論是在哪裡執行 C# 應用程式,幾乎都能收到錯誤報告)。
如要進一步瞭解安裝,請參閱 Google.Cloud.Diagnostics 程式庫集的說明文件。您也可以使用問題追蹤工具報告問題。
設定用戶端程式庫
如需更多資訊和範例,請參閱下列資源:
在 Google Cloud上執行應用程式
如要使用 projects.events.report
建立錯誤群組,服務帳戶必須具備Error Reporting 寫入者角色 (roles/errorreporting.writer
)。
部分 Google Cloud 服務會自動將 Error Reporting 寫入者角色 (roles/errorreporting.writer
) 授予適當的服務帳戶。不過,您必須為某些服務的適當服務帳戶授予這個角色。
Cloud Run 和 Cloud Run 函式
Cloud Run 使用的預設服務帳戶具有 Error Reporting 寫入者角色 (roles/errorreporting.writer
) 的權限。
不需要明確提供憑證,即可使用 Google.Cloud.Diagnostics 程式庫集。
Cloud Run 設定為自動使用 Error Reporting。未處理的 JavaScript 例外狀況會出現在 Logging 之中並由 Error Reporting 處理,不需使用 Google.Cloud.Diagnostics 程式庫集。
App Engine 彈性環境
App Engine 會自動將 Error Reporting 寫入者角色 (roles/errorreporting.writer
) 授予預設服務帳戶。
不需要明確提供憑證,即可使用 Google.Cloud.Diagnostics 程式庫集。
系統會自動為 App Engine 彈性環境應用程式啟用 Error Reporting。無須額外設定。系統會分析從 App Engine 寫入 stderr
的訊息,如果發現含有堆疊追蹤,就會顯示在 Error Reporting 中。
如需在Google Cloud 主控台中查看應用程式的錯誤和例外狀況資料的操作說明,請參閱「查看錯誤」一文。
Google Kubernetes Engine
如要在 Google Kubernetes Engine 中使用 Error Reporting,請按照下列步驟操作:
Compute Engine
如要搭配 Compute Engine VM 執行個體使用錯誤報告,請完成下列步驟:
範例
如需更多資訊和範例,請參閱下列資源:
在本機開發環境中執行應用程式
如要在本機開發環境中使用 Google.Cloud.Diagnostics 程式庫集 (例如在自己的工作站上執行程式庫),您必須為 Google.Cloud.Diagnostics 程式庫集提供本機應用程式預設憑證。詳情請參閱「向 Error Reporting 進行驗證」。
如要在本機開發環境中使用本頁的 .NET 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
安裝 Google Cloud CLI。
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Set up authentication for a local development environment。
projects.events.report
方法也支援 API 金鑰。
如要使用 API 金鑰進行驗證,您不需要設定本機應用程式預設憑證檔案。
詳情請參閱
請參閱 Google Cloud 驗證說明文件,建立 API 金鑰。
查看錯誤報告
前往 Google Cloud 控制台的「Error Reporting」頁面:
您也可以透過搜尋列找到這個頁面。
詳情請參閱查看錯誤一文。