把报错信息通过服务上传

开发者总是试图创造没有 bug 的应用,但是 bug 还是会时不时地出现。这些 bug 会给用户带来糟糕的体验,所以获知 bug 发生的位置以及出现的频率就显得极为关键了。这样,你就可以根据 bug 的影响程度优先修复它们。

While one always tries to create apps that are free of bugs, they’re sure to crop up from time to time. Since buggy apps lead to unhappy users and customers, it’s important to understand how often your users experience bugs and where those bugs occur. That way, you can prioritize the bugs with the highest impact and work to fix them.

如何确定用户遇到 bug 的频率呢?解决方案是:当异常发生时,生成一份日志,日志中包含发生的异常及相关的堆栈信息。随后,可以将日志发送到异常监控报警平台,比如 Sentry、Fabric 或者 Rollbar。

How can you determine how often your users experiences bugs? Whenever an error occurs, create a report containing the error that occurred and the associated stacktrace. You can then send the report to an error tracking service, such as Sentry, Fabric, or Rollbar.

异常监控报警平台会将上报的崩溃日志异常信息聚合并分组归类,这样就可以知道应用程序出现异常的频率并定位异常发生位置。

The error tracking service aggregates all of the crashes your users experience and groups them together. This allows you to know how often your app fails and where the users run into trouble.

这个章节中,你可以通过以下步骤学习如何把异常信息上报给异常监控报警平台 Sentry

In this recipe, learn how to report errors to the Sentry crash reporting service using the following steps:

  1. 从 Sentry 平台获取 DSN

    Get a DSN from Sentry.

  2. 导入 Sentry package

    Import the Flutter Sentry package

  3. 初始化 Sentry SDK

    Initialize the Sentry SDK

  4. 捕获并上报异常

    Capture errors programmatically

1. 从 Sentry 平台获取 DSN

1. Get a DSN from Sentry

在向 Sentry 上报异常信息前,需要在 Sentry.io 上获取应用的唯一身份标识 DSN。

Before reporting errors to Sentry, you need a “DSN” to uniquely identify your app with the Sentry.io service.

根据以下步骤,获取 DSN:

To get a DSN, use the following steps:

  1. 创建 Sentry 账户

    Create an account with Sentry.

  2. 登录账户

    Log in to the account.

  3. 新建一个 Flutter 工程

    Create a new Flutter project.

  4. 复制包含 DSN 的代码片段

    Copy the code snippet that includes the DSN.

2. 导入 Sentry 包

2. Import the Sentry package

导入 sentry_flutter package 到应用中,这个 package 能更方便的将错误报告发送给 Sentry 的错误追踪系统。

Import the sentry_flutter package into the app. The sentry package makes it easier to send error reports to the Sentry error tracking service.

dependencies:
  sentry_flutter: <latest_version>

3. 创建 SentryClient

3. Initialize the Sentry SDK

初始化 SDK 来自动捕获不同的未处理的错误。

Initialize the SDK to capture different unhandled errors automatically:

import 'package:flutter/widgets.dart';
import 'package:sentry_flutter/sentry_flutter.dart';

Future<void> main() async {
  await SentryFlutter.init(
    (options) => options.dsn = 'https://example@sentry.io/example',
    appRunner: () => runApp(MyApp()),
  );
}

另外,你也可以使用 dart-define 标记将 DSN 传递给 Flutter。

Alternatively, you can pass the DSN to Flutter using the dart-define tag:

--dart-define SENTRY_DSN=https://example@sentry.io/example

这番操作都发生了什么

What does that give me?

使用 Sentry 捕获 Dart 和原生中未处理的错误,以上这些操作就足够了。这包括 iOS 上的 Swift、Objective-C、C 和 C++,以及 Android 上的 Java、Kotlin、C 和 C++。

This is all you need for Sentry to capture unhandled errors in Dart and native layers.
This includes Swift, Objective-C, C, and C++ on iOS, and Java, Kotlin, C, and C++ on Android.

4. 捕获并上报异常

4. Capture errors programatically

除了自动初始化 Sentry SDK 来捕获和上报错误之外,你还可以通过 API 来报告错误:

Besides the automatic error reporting that Sentry generates by importing and initializing the SDK, you can use the API to report errors to Sentry:

await Sentry.captureException(exception, stackTrace: stackTrace);

完整的 API 可在 pub.dev 上阅读。

The complete API is available on pub.dev.

了解更多

Learn more

更多关于使用 Sentry SDK 的文档可以在 其官网 查看。

Extensive documentation about using the Sentry SDK can be found on Sentry’s site.

完整样例

Complete example

查看 Crashy 示例应用,体验完整流程。

To view a working example, see the Crashy example app.