這很重要
Visual Studio App Center 於 2025 年 3 月 31 日淘汰,但分析和診斷功能除外,這些功能將持續支援到 2026 年 6 月 30 日。 瞭解更多資訊。
App Center 崩潰會在每次應用程式當機時自動產生當機記錄檔。 記錄會先寫入裝置的記憶體,當使用者再次啟動應用程式時,當機報告會傳送至 App Center。 收集崩潰報告適用於 Beta 版和正式版本應用程式,亦即提交至 Google Play 的應用程式。 當機記錄包含協助您修正當機的重要資訊。
如果您尚未在應用程式中設定 SDK,請遵循 開始使用 一節。
無論您在何處使用 App Center 當機,請在檔案頂端新增下列匯入。
// Import App Center Crashes at the top of the file.
import Crashes from 'appcenter-crashes';
產生測試當機
App Center 當機提供 API 來產生測試當機,以便輕鬆測試 SDK。 此 API 只能用於測試/Beta 應用程式,且不會在生產應用程式中執行任何動作。
Crashes.generateTestCrash();
也很容易產生 JavaScript 當機。 將下列這一行新增至您的程序代碼,這會擲回 JavaScript 錯誤並造成當機。
throw new Error('This is a test javascript crash!');
小提示
您的 React Native 應用程式必須在 發行模式 中編譯,才能將此當機傳送至 App Center。
備註
目前,App Center 不支援來源地圖,以解除縮小 Android React Native 應用程式的 JavaScript 堆疊追踪。
備註
最佳做法是避免使用字串值 (例如: throw) 的 JavaScript throw 'message' 語句,因為 React Native 在此案例中不會保留完整的 JavaScript 堆棧。 相反地, throw JavaScript Error (例如: throw Error('message'))。
取得先前當機的詳細資訊
App Center Crashes 有兩個 API,可以在應用程式當機時提供更多資訊。
應用程式在上一個會話中是否收到記憶體不足的警告?
在啟動 SDK 之後,您可以隨時檢查應用程式是否在上一個工作階段中收到記憶體警告:
const hadLowMemoryWarning = await Crashes.hasReceivedMemoryWarningInLastSession();
備註
在某些情況下,記憶體不足的裝置可能不會傳送事件。
應用程式在上一個工作階段中是否當機?
在啟動 SDK 之後,您可以隨時檢查應用程式是否在上一次啟動中當機:
const didCrash = await Crashes.hasCrashedInLastSession();
如果您想要在發生當機后調整應用程式的行為或 UI,這會派上用場。 有些開發人員選擇顯示額外的 UI 來向使用者道歉,或者希望在發生當機後找到方法與他們聯繫。
上次當機的詳細數據
如果您的應用程式先前當機,您可以取得上次當機的詳細數據。
const crashReport = await Crashes.lastSessionCrashReport();
自訂 App Center 當機的使用方式
App Center 當機提供回呼,讓開發人員在將損毀記錄傳送至 App Center 之前和時執行其他動作。
在 JavaScript 中處理當機
為了讓 Crash.setListener 方法如預期般運作,您需要檢查應用程式是否已正確設定。
- 開啟項目的
ios/YourAppName/AppDelegate.m檔案,並確認您有[AppCenterReactNativeCrashes register];,而不是[AppCenterReactNativeCrashes registerWithAutomaticProcessing];。 - 開啟項目的
android/app/src/main/res/values/strings.xml檔案,並確認 已appCenterCrashes_whenToSendCrashes設定為ASK_JAVASCRIPT。
此檔中會逐一討論事件接聽程式的所有不同回呼,但您必須設定一個事件接聽程式,以一次定義所有回呼。
是否應該處理當機?
如果您想要決定是否需要處理特定的當機,請實作此回呼。 例如,您可能會想要忽略系統層級的崩潰,而且您也不希望將其傳送至 App Center。
Crashes.setListener({
shouldProcess: function (report) {
return true; // return true if the crash report should be processed, otherwise false.
},
// Other callbacks must also be defined at the same time if used.
// Default values are used if a method with return parameter isn't defined.
});
要求使用者同意傳送當機記錄檔
如果用戶隱私權對於您很重要,您應該先取得使用者確認,再將當機報告傳送至 App Center。 SDK 會公開回呼,告知 App Center 損毀在傳送任何損毀報告之前等候用戶確認。
如果您選擇這樣做,您必須負責取得使用者的確認,例如透過具有下列其中一個選項的對話框提示: 永遠傳送、 傳送和 不要傳送。 根據輸入,您將告訴 App Center 崩潰需執行的動作,然後會據以處理崩潰。
備註
SDK 不會顯示此對話框,應用程式必須提供自己的 UI 以要求使用者同意。
下列回呼顯示如何告訴 SDK 等到使用者確認之後,再傳送錯誤回報:
Crashes.setListener({
shouldAwaitUserConfirmation: function (report) {
// Build your own UI to ask for user consent here. SDK doesn't provide one by default.
// Return true if you built a UI for user consent and are waiting for user input on that custom UI, otherwise false.
return true;
},
// Other callbacks must also be defined at the same time if used.
// Default values are used if a method with return parameter isn't defined.
});
如果您傳回 true,您的應用程式必須使用您自己的程式碼來取得使用者的許可,並使用下列 API 將結果更新到 SDK:
import Crashes, { UserConfirmation } from 'appcenter-crashes';
// Depending on the user's choice, call Crashes.notifyUserConfirmation() with the right value.
Crashes.notifyUserConfirmation(UserConfirmation.DONT_SEND);
Crashes.notifyUserConfirmation(UserConfirmation.SEND);
Crashes.notifyUserConfirmation(UserConfirmation.ALWAYS_SEND);
備註
若要使用這項功能,請針對當機服務正確設定您的應用程式。 此功能取決於 JavaScript 中的崩潰處理。
取得當機記錄檔傳送狀態的相關資訊
有時,您想要知道應用程式當機狀態。 常見的使用案例是,您可能會想要顯示UI,告知使用者您的應用程式正在提交當機報告,或者,如果app在啟動後快速當機,您想要調整應用程式的行為,以確保可以提交當機記錄。 App Center Crashes 提供三個不同的回呼,您可以在應用程式中用來了解發生的狀況。
若要這樣做,請在程式代碼中定義事件接聽程式,如下列範例所示:
Crashes.setListener({
onBeforeSending: function (report) {
// called after Crashes.process and before sending the crash.
},
onSendingSucceeded: function (report) {
// called when crash report sent successfully.
},
onSendingFailed: function (report) {
// called when crash report couldn't be sent.
}
// Other callbacks must also be defined at the same time if used.
// Default values are used if a method with return parameter isn't defined.
});
所有回呼都是選擇性的。 您不需要在事件接聽程式物件中提供所有 3 種方法,例如您只能 onBeforeSending實作 。
備註
如果 Crashes.setListener 呼叫多次,則最後一個會獲勝;它會覆寫先前由 所設定的 Crashes.setListener接聽程式。
onSendingFailed接收表示發生無法復原的錯誤,例如發生 4xx 碼。 例如, 401 表示 appSecret 錯誤。
若是網路問題,則不會觸發此回呼。 在此情況下,SDK 會持續重試(並在網路連線關閉時暫停重試)。 如果我們在端點上有網路問題或中斷,而且您重新啟動應用程式, onBeforeSending 則會在進程重新啟動后再次觸發。
將附件新增至當機報表
您可以將二進位和文字附件新增至當機報表。 SDK 會連同當機一起傳送它們,讓您可以在 App Center 入口網站中看到它們。 在發送先前應用程式啟動時儲存的當機紀錄之前,下列回呼將會被叫用。 當系統崩潰時,不會被調用。 請確定附件檔案 未 被命名為 minidump.dmp,因為該名稱是保留給迷你傾印檔案使用的。 以下是將文字和圖片附加至故障報告的範例:
import Crashes, { ErrorAttachmentLog } from 'appcenter-crashes';
Crashes.setListener({
getErrorAttachments(report) {
const textAttachment = ErrorAttachmentLog.attachmentWithText('Hello text attachment!', 'hello.txt');
const binaryAttachment = ErrorAttachmentLog.attachmentWithBinary(`${imageAsBase64string}`, 'logo.png', 'image/png');
return [textAttachment, binaryAttachment];
}
// Other callbacks must also be defined at the same time if used.
// Default values are used if a method with return parameter isn't defined.
});
參數 fileName 是選擇性的 (可以是 null),而且只能在App Center入口網站中使用。 您可以從入口網站中的特定當機事件查看並下載附件。 如果您指定了檔名,則使用該檔名下載,否則系統會為您產生檔名。
若要設定 getErrorAttachments 回呼以使用ES2017 async/await 函式,請改為傳回已完成的 Promise。 下列範例會以非同步方式附加文字和圖片至系統當機:
import Crashes, { ErrorAttachmentLog } from 'appcenter-crashes';
Crashes.setListener({
getErrorAttachments(report) {
return (async () => {
const textContent = await readTextFileAsync(); // use your async function to read text file
const binaryContent = await readBinaryFileAsync(); // use your async function to read binary file
const textAttachment = ErrorAttachmentLog.attachmentWithText(textContent, 'hello.txt');
const binaryAttachment = ErrorAttachmentLog.attachmentWithBinary(binaryContent, 'logo.png', 'image/png');
return [textAttachment, binaryAttachment];
})();
}
// Other callbacks must also be defined at the same time if used.
// Default values are used if a method with return parameter isn't defined.
});
備註
Android 的大小限制目前為 1.4 MB,而 iOS 的大小限製為 7 MB。 嘗試傳送較大的附件將會觸發錯誤。
已處理的錯誤
App Center 也可讓您透過 trackError 方法使用已處理的例外狀況來追蹤錯誤。 應用程式可以選擇性地將屬性或/和附件附加至已處理的錯誤報告,以提供進一步的內容。
try {
// Throw error.
} catch (error) {
// Prepare properties.
const properties = { 'Category' : 'Music', 'Wifi' : 'On' };
// Prepare attachments.
const textAttachment = ErrorAttachmentLog.attachmentWithText('Hello text attachment!', 'hello.txt');
const attachments = [textAttachment];
// Create an exception model from error.
const exceptionModel1 = ExceptionModel.createFromError(error);
// ..or generate with your own error data.
const exceptionModel2 = ExceptionModel.createFromTypeAndMessage("type", "message", "stacktrace");
// Track error with custom data.
Crashes.trackError(exceptionModel1, properties, attachments);
Crashes.trackError(exceptionModel1, properties, nil);
Crashes.trackError(exceptionModel2, nil, attachments);
Crashes.trackError(exceptionModel2, nil, nil);
}
Breakpad
App Center 支援 React Native 應用程式中使用 Android NDK 的 Breakpad 異常。
遵循上述的一般設定步驟,並在 MainActivity.java 中覆寫 OnCreate,新增小型傾印組態,並呼叫進入您的原生程式碼以設定 Breakpad 組態。
範例:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Crashes.getMinidumpDirectory().thenAccept(new AppCenterConsumer<String>() {
@Override
public void accept(String path) {
// Path is null when Crashes is disabled.
if (path != null) {
// links to NDK
setupBreakpadListener(path);
}
}
});
}
在運行時間啟用或停用 App Center 當機
您可以在執行時間啟用和停用 App Center 崩潰功能。 如果您將其停用,SDK 將不會針對應用程式執行任何當機報告。
await Crashes.setEnabled(false);
若要再次啟用 App Center 異常回報功能,請使用相同的 API,但傳遞 true 作為參數。
await Crashes.setEnabled(true);
狀態會保存在裝置跨應用程式啟動時的記憶體中。
檢查應用程式中心崩潰是否已啟用
您也可以檢查 App Center 損毀是否已啟用:
const enabled = await Crashes.isEnabled();