crashReporter
向远程服务器提交崩溃报告。
如果你想从启用上下文隔离的渲染进程调用此 API,
请将 API 调用放在你的 preload 脚本中,并
使用 contextBridge API 暴露 它。
以下是一个设置 Electron 自动将崩溃报告提交到远程服务器的示例:
🌐 The following is an example of setting up Electron to automatically submit crash reports to a remote server:
const { crashReporter } = require('electron')
crashReporter.start({ submitURL: 'https://your-domain.com/url-to-submit' })
有关收集、接收和符号化崩溃报告的指南,包括如何运行自己的崩溃服务器或使用托管服务,请参阅崩溃报告教程。
🌐 For a guide to collecting, receiving and symbolicating crash reports, including how to run your own crash server or use a hosted service, see the Crash Reporting tutorial.
Electron 使用 Crashpad 来监控和报告崩溃。Crashpad 使用和 Breakpad 一样的 上传协议,所以接受 Breakpad minidump 的服务器也能接收 Electron 的崩溃报告。
🌐 Electron uses Crashpad to monitor and report crashes. Crashpad uses the same upload protocol as Breakpad, so servers that accept Breakpad minidumps can receive Electron's crash reports.
崩溃报告存储在 app.getPath('crashDumps') 返回的目录下。你可以在启动崩溃报告程序之前通过调用 app.setPath('crashDumps', '/path/to/crashes') 来重写它。这个目录中文件的布局是实现细节,可能会随 Electron 的版本变化而改变。
🌐 Crash reports are stored under the directory returned by
app.getPath('crashDumps'). You can override it by calling
app.setPath('crashDumps', '/path/to/crashes') before starting the crash
reporter. The layout of files inside this directory is an implementation detail
and may change between versions of Electron.
crashReporter 模块在 Mac 应用商店版本中被禁用。它的方法可以被调用,但不会有任何作用:不会收集或上传崩溃报告,getUploadedReports() 返回一个空数组,getUploadToServer() 返回 false。
🌐 The crashReporter module is disabled in Mac App Store builds. Its methods can
be called, but they do nothing: no crash reports are collected or uploaded,
getUploadedReports() returns an empty array and
getUploadToServer() returns
false.
方法
🌐 Methods
crashReporter 模块具有以下方法:
🌐 The crashReporter module has the following methods:
crashReporter.start(options)
History
| Version(s) | Changes |
|---|---|
None | Added |
None | Deprecated calling this method in the renderer process. |
None | Default value of |
None | The |
在使用任何其他 crashReporter API 之前,必须调用此方法。一旦以这种方式初始化,crashpad 处理程序将收集随后创建的所有进程的崩溃信息。一旦启动,崩溃报告程序无法禁用。
🌐 This method must be called before using any other crashReporter APIs. Once
initialized this way, the crashpad handler collects crashes from all
subsequently created processes. The crash reporter cannot be disabled once
started.
此方法应在应用启动时尽早调用,最好在 app.on('ready') 之前。如果在创建渲染进程时崩溃报告程序尚未初始化,那么该渲染进程将不会被崩溃报告程序监控。
🌐 This method should be called as early as possible in app startup, preferably
before app.on('ready'). If the crash reporter is not initialized at the time
a renderer process is created, then that renderer process will not be monitored
by the crash reporter.
你可以通过使用 process.crash() 生成崩溃来测试崩溃报告器。
如果在第一次调用 start 之后需要发送额外的或更新的 extra 参数,可以调用 addExtraParameter。
通过 extra、globalExtra 传入或者通过 addExtraParameter 设置的参数,键和值都有长度限制。键名最长不能超过 39 字节,值最长不能超过 20320 字节。超过最大长度的键名会被忽略,并会发出警告。超过最大长度的值会被截断。
此方法仅在主进程中可用。
crashReporter.getLastCrashReport()
History
| Version(s) | Changes |
|---|---|
None | Deprecated calling this method in the renderer process. |
返回 CrashReport | null - 从 getUploadedReports() 返回的列表中,上传时间最新的崩溃报告的日期和 ID。如果根本没有崩溃报告,则返回 null。
🌐 Returns CrashReport | null - The date and ID of the
crash report with the most recent upload time, from the list returned by
getUploadedReports(). If there are no crash
reports at all, null is returned.
如果还没有上传报告,但有些报告存储在磁盘上,可能会返回尚未上传的报告。在将其视为已上传之前,先检查它的 id 是否为空。
🌐 If no report has been uploaded yet but some are stored on disk, a report that has
not been uploaded may be returned. Check that its id is not empty before
treating it as uploaded.
此方法仅在主进程中可用。
crashReporter.getUploadedReports()
History
| Version(s) | Changes |
|---|---|
None | Deprecated calling this method in the renderer process. |
返回 CrashReport[]:
🌐 Returns CrashReport[]:
返回存储在磁盘上的崩溃报告。每个报告都包含上传日期和崩溃服务器返回的ID。
🌐 Returns the crash reports stored on disk. Each report contains the date it was uploaded and the ID that the crash server returned for it.
尽管方法的名称如此,但那些没有被上传的报告(例如因为 uploadToServer 是 false、上传失败,或者报告被限速)也会被包含在内。对于这些报告,id 是空字符串,date 没有实际意义。要只列出已上传的报告,可以过滤掉 id 为空的报告。
🌐 Despite the method's name, reports that have not been uploaded (for example
because uploadToServer is false, the upload failed, or the report was rate
limited) are included too. For those reports, id is an empty string and date
is not meaningful. To list only uploaded reports, filter out reports with an
empty id.
此方法仅在主进程中可用。
crashReporter.getUploadToServer()
History
| Version(s) | Changes |
|---|---|
None | Deprecated calling this method in the renderer process. |
返回 boolean - 是否应将报告提交到服务器。通过 start 方法或 setUploadToServer 设置。
🌐 Returns boolean - Whether reports should be submitted to the server. Set through
the start method or setUploadToServer.
此方法仅在主进程中可用。
crashReporter.setUploadToServer(uploadToServer)
History
| Version(s) | Changes |
|---|---|
None | Deprecated calling this method in the renderer process. |
uploadToServerboolean - 是否应将报告提交到服务器。
这通常由用户偏好控制。如果在调用 start 之前调用,则不会生效。
🌐 This would normally be controlled by user preferences. This has no effect if
called before start is called.
此方法仅在主进程中可用。
crashReporter.addExtraParameter(key, value)
keystring - 参数键,长度不得超过39字节。valuestring - 参数值,不能超过20320字节。
设置一个额外的参数随崩溃报告一起发送。这里指定的值会在调用 start 时,除了通过 extra 选项设置的值外一并发送。再次使用相同的键调用会替换该值。这个值会在发生崩溃时读取,所以你可以随着应用状态的变化更新它。
🌐 Set an extra parameter to be sent with the crash report. The values specified
here will be sent in addition to any values set via the extra option when
start was called. Calling this again with the same key replaces the value.
The value is read when a crash happens, so you can update it as your app's
state changes.
以这种方式添加的参数(或通过 crashReporter.start 的 extra 参数)是特定于调用进程的。在主进程中添加额外的参数不会导致这些参数随渲染器或其他子进程的崩溃一起发送。同样,在渲染器进程中添加额外参数,也不会导致这些参数随其他渲染器进程或主进程发生的崩溃发送。使用 utilityProcess 创建的进程没有设置额外参数的 API,所以它们崩溃时只会发送 globalExtra 的值。
🌐 Parameters added in this fashion (or via the extra parameter to
crashReporter.start) are specific to the calling process. Adding extra
parameters in the main process will not cause those parameters to be sent along
with crashes from renderer or other child processes. Similarly, adding extra
parameters in a renderer process will not result in those parameters being sent
with crashes that occur in other renderer processes or in the main process.
Processes created with utilityProcess have no API for
setting extra parameters, so only globalExtra values are sent with their
crashes.
参数在键和值的长度上有一定限制。键名不能超过39字节,值不能超过20320字节。键名超过最大长度的会被忽略,并且会发出警告。值超过最大长度的会被截断。
crashReporter.removeExtraParameter(key)
keystring - 参数键,长度不得超过39字节。
从当前参数集中移除一个多余的参数。将来的崩溃中不会包含此参数。
🌐 Remove an extra parameter from the current set of parameters. Future crashes will not include this parameter.
crashReporter.getParameters()
返回 Record<string, string> - 调用进程中崩溃报告器的当前“额外”参数,这些参数是通过 extra 选项和 addExtraParameter 设置的。通过 globalExtra 选项设置的参数不包括在内。
🌐 Returns Record<string, string> - The current 'extra' parameters of the crash
reporter in the calling process, as set with the extra option and
addExtraParameter. Parameters set with the globalExtra option are not
included.
在 Node 子进程中
🌐 In Node child processes
由于 require('electron') 在 Node 子进程中不可用(以 ELECTRON_RUN_AS_NODE 运行的进程,例如使用 child_process.fork() 创建的进程),因此在 Node 子进程中可以通过 process 对象使用以下 API。
🌐 Since require('electron') is not available in Node child processes (processes
run with ELECTRON_RUN_AS_NODE, such as those created with
child_process.fork()), the following APIs are available on the process
object in Node child processes.
如果在主进程中启动崩溃报告程序,Node 子进程会自动被监控。无法从 Node 子进程启动崩溃报告程序。
🌐 If the crash reporter is started in the main process, Node child processes are monitored automatically. There is no way to start the crash reporter from a Node child process.
process.crashReporter.getParameters()
请参见 crashReporter.getParameters()。
🌐 See crashReporter.getParameters().
process.crashReporter.addExtraParameter(key, value)
请参见 crashReporter.addExtraParameter(key, value)。
🌐 See crashReporter.addExtraParameter(key, value).
process.crashReporter.removeExtraParameter(key)
请参见 crashReporter.removeExtraParameter(key)。
🌐 See crashReporter.removeExtraParameter(key).
崩溃报告有效负载
🌐 Crash Report Payload
崩溃报告程序将以 multipart/form-data POST 的形式将以下数据发送给 submitURL。除非 compress 是 false,否则请求体会进行 gzip 压缩并随 Content-Encoding: gzip 一起发送。
🌐 The crash reporter will send the following data to the submitURL as
a multipart/form-data POST. Unless compress is false, the request body
is gzip-compressed and sent with Content-Encoding: gzip.
verstring - Electron 的版本。platformstring - 例如 'win32'。process_typestring - 例如,主进程可以是“renderer”或“browser”。guidstring - 例如 '5e1286fc-da97-479e-918b-6bfb0c3d1c72'。_versionstring -package.json中的版本。_productNamestring -crashReporteroptions对象中的产品名称。prodstring - 基础产品的名称。在此情况下是 Electron。_companyNamestring -crashReporteroptions对象中的公司名称。仅在设置了已弃用的companyName选项时发送。upload_file_minidumpFile - 以minidump格式的崩溃报告。crashReporteroptions对象中globalExtra对象的所有一级属性。- 所有导致进程崩溃的额外参数,都可以通过
extra选项(仅主进程)或addExtraParameter设置。
Crashpad 和 Chromium 可能会在上传时添加其他字段。这些字段不是 Electron API 的一部分,可能随时更改,所以不要依赖它们。
🌐 Crashpad and Chromium may add other fields to the upload. These are not part of Electron's API and can change without notice, so don't rely on them.
服务器响应的内容被存储为崩溃报告的 ID,并通过 getUploadedReports() 返回在 id 字段中。
🌐 The body of the server's response is stored as the crash report's ID, and is
returned in the id field by
getUploadedReports().