crashReporter
将崩溃报告提交到远程服务器。
¥Submit crash reports to a remote server.
[!重要] 如果你想从启用了上下文隔离的渲染进程调用此 API,请将 API 调用放在预加载脚本中,并使用
contextBridgeAPI 对其进行 expose 操作。¥[!IMPORTANT] If you want to call this API from a renderer process with context isolation enabled, place the API call in your preload script and expose it using the
contextBridgeAPI.
以下是设置 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 setting up a server to accept and process crash reports, you can use following projects:
[!NOTE] Electron 使用 Crashpad 而不是 Breakpad 来收集和上传崩溃数据,但目前使用 上传协议相同。
¥[!NOTE] Electron uses Crashpad, not Breakpad, to collect and upload crashes, but for the time being, the upload protocol is the same.
或者使用第三方托管解决方案:
¥Or use a 3rd party hosted solution:
崩溃报告在上传到应用用户数据目录下的目录(名为 '崩溃垫')之前会暂时存储。你可以在启动崩溃报告器之前调用 app.setPath('crashDumps', '/path/to/crashes') 来覆盖此目录。
¥Crash reports are stored temporarily before being uploaded in a directory
underneath the app's user data directory, called 'Crashpad'. You can override
this directory by calling app.setPath('crashDumps', '/path/to/crashes')
before starting the crash reporter.
Electron 使用 crashpad 来监视和报告崩溃。
¥Electron uses crashpad to monitor and report crashes.
方法
¥Methods
crashReporter 模块有以下方法:
¥The crashReporter module has the following methods:
crashReporter.start(options)
在使用任何其他 crashReporter API 之前必须调用此方法。一旦以这种方式初始化,崩溃处理程序就会从所有后续创建的进程中收集崩溃。崩溃报告器一旦启动就无法禁用。
¥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.
[!NOTE] 你可以使用
process.crash()生成崩溃报告来测试崩溃报告器。¥[!NOTE] You can test out the crash reporter by generating a crash using
process.crash().
[!NOTE] 如果你需要在第一次调用
start后发送额外的/更新的extra参数,你可以调用addExtraParameter。¥[!NOTE] If you need to send additional/updated
extraparameters after your first callstartyou can calladdExtraParameter.
[!NOTE] 在
extra、globalExtra中传递的参数或使用addExtraParameter设置的参数的键和值的长度有限制。键名称的长度不得超过 39 个字节,值的长度不得超过 127 个字节。名称长于最大值的键将被默默忽略。超过最大长度的键值将被截断。¥[!NOTE] Parameters passed in
extra,globalExtraor set withaddExtraParameterhave limits on the length of the keys and values. Key names must be at most 39 bytes long, and values must be no longer than 127 bytes. Keys with names longer than the maximum will be silently ignored. Key values longer than the maximum length will be truncated.
[!NOTE] 此方法仅在主进程中可用。
¥[!NOTE] This method is only available in the main process.
crashReporter.getLastCrashReport()
返回 CrashReport | null - 上次崩溃报告的日期和 ID。仅返回已上传的崩溃报告;即使磁盘上存在崩溃报告,在上传之前也不会返回该报告。如果没有上传的报告,则返回 null。
¥Returns CrashReport | null - The date and ID of the
last crash report. Only crash reports that have been uploaded will be returned;
even if a crash report is present on disk it will not be returned until it is
uploaded. In the case that there are no uploaded reports, null is returned.
[!NOTE] 此方法仅在主进程中可用。
¥[!NOTE] This method is only available in the main process.
crashReporter.getUploadedReports()
返回 CrashReport[]:
¥Returns CrashReport[]:
返回所有上传的崩溃报告。每份报告均包含日期和上传的 ID。
¥Returns all uploaded crash reports. Each report contains the date and uploaded ID.
[!NOTE] 此方法仅在主进程中可用。
¥[!NOTE] This method is only available in the main process.
crashReporter.getUploadToServer()
返回 boolean - 是否应将报告提交到服务器。通过 start 方法或 setUploadToServer 方法设置。
¥Returns boolean - Whether reports should be submitted to the server. Set through
the start method or setUploadToServer.
[!NOTE] 此方法仅在主进程中可用。
¥[!NOTE] This method is only available in the main process.
crashReporter.setUploadToServer(uploadToServer)
-
uploadToServer布尔值 - 是否应将报告提交到服务器。¥
uploadToServerboolean - Whether reports should be submitted to the server.
这通常由用户偏好控制。如果在调用 start 之前调用,这不会产生任何影响。
¥This would normally be controlled by user preferences. This has no effect if
called before start is called.
[!NOTE] 此方法仅在主进程中可用。
¥[!NOTE] This method is only available in the main process.
crashReporter.addExtraParameter(key, value)
-
key字符串 - 参数 key,不能超过 39 字节。¥
keystring - Parameter key, must be no longer than 39 bytes. -
value字符串 - 参数值,不得超过 127 字节。¥
valuestring - Parameter value, must be no longer than 127 bytes.
设置与崩溃报告一起发送的额外参数。当调用 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.
以这种方式添加的参数(或通过 extra 参数添加到 crashReporter.start)特定于调用进程。在主进程中添加额外的参数不会导致这些参数与渲染器或其他子进程崩溃一起发送。同样,在渲染器进程中添加额外的参数不会导致发送这些参数时发生其他渲染器进程或主进程中发生的崩溃。
¥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.
[!NOTE] 参数的键和值的长度有限制。键名称不得超过 39 字节,值不得超过 20320 字节。名称长于最大值的键将被默默忽略。超过最大长度的键值将被截断。
¥[!NOTE] Parameters have limits on the length of the keys and values. Key names must be no longer than 39 bytes, and values must be no longer than 20320 bytes. Keys with names longer than the maximum will be silently ignored. Key values longer than the maximum length will be truncated.
crashReporter.removeExtraParameter(key)
-
key字符串 - 参数 key,不能超过 39 字节。¥
keystring - Parameter key, must be no longer than 39 bytes.
从当前参数集中删除额外的参数。未来的崩溃将不包含此参数。
¥Remove an extra parameter from the current set of parameters. Future crashes will not include this parameter.
crashReporter.getParameters()
返回 Record<string, string> - 崩溃报告器的当前 'extra' 参数。
¥Returns Record<string, string> - The current 'extra' parameters of the crash reporter.
在 Node 子进程中
¥In Node child processes
由于 require('electron') 在 Node 子进程中不可用,因此以下 API 在 Node 子进程中的 process 对象上可用。
¥Since require('electron') is not available in Node child processes, the
following APIs are available on the process object in Node child processes.
process.crashReporter.start(options)
¥See crashReporter.start().
请注意,如果崩溃报告器在主进程中启动,它将自动监视子进程,因此不应在子进程中启动它。仅当主进程未初始化崩溃报告器时才使用此方法。
¥Note that if the crash reporter is started in the main process, it will automatically monitor child processes, so it should not be started in the child process. Only use this method if the main process does not initialize the crash reporter.
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:
¥The crash reporter will send the following data to the submitURL as
a multipart/form-data POST:
-
ver字符串 - Electron 的版本。¥
verstring - The version of Electron. -
platform字符串 - 例如 'win32'。¥
platformstring - e.g. 'win32'. -
process_type字符串 - 例如 'renderer'。¥
process_typestring - e.g. 'renderer'. -
guid字符串 - 例如 '5e1286fc-da97-479e-918b-6bfb0c3d1c72'。¥
guidstring - e.g. '5e1286fc-da97-479e-918b-6bfb0c3d1c72'. -
_version字符串 -package.json版本。¥
_versionstring - The version inpackage.json. -
_productName字符串 -crashReporteroptions对象中的产品名称。¥
_productNamestring - The product name in thecrashReporteroptionsobject. -
prod字符串 - 基础产品的名称。在本例中是 Electron。¥
prodstring - Name of the underlying product. In this case Electron. -
_companyName字符串 -crashReporteroptions对象中的公司名称。¥
_companyNamestring - The company name in thecrashReporteroptionsobject. -
upload_file_minidump文件 - 崩溃报告的格式为minidump。¥
upload_file_minidumpFile - The crash report in the format ofminidump. -
crashReporteroptions对象中extra对象的所有一级属性。¥All level one properties of the
extraobject in thecrashReporteroptionsobject.