1# Subscribing to Crash Events (ArkTS)
2
3## Available APIs
4
5For details about how to use the APIs (such as parameter usage constraints and value ranges), see [Application Event Logging](../reference/apis-performance-analysis-kit/js-apis-hiviewdfx-hiappevent.md).
6
7**API for Setting Custom Event Parameters**
8
9| API                                             | Description                                        |
10| --------------------------------------------------- | -------------------------------------------- |
11| setEventParam(params: Record<string, ParamType>, domain: string, name?: string): Promise<void> | Sets custom event parameters.|
12
13**Subscription APIs**
14
15| API                                             | Description                                        |
16| --------------------------------------------------- | -------------------------------------------- |
17| addWatcher(watcher: Watcher): AppEventPackageHolder | Adds a watcher to listen for application events.|
18| removeWatcher(watcher: Watcher): void               | Removes a watcher to unsubscribe from application events.|
19
20## How to Develop
21
22The following describes how to subscribe to a crash event triggered by a button click.
23
241. Create an ArkTS application project. In the **entry/src/main/ets/entryability/EntryAbility.ets** file, import the dependent modules.
25
26   ```ts
27   import { BusinessError } from '@kit.BasicServicesKit';
28   import { hiAppEvent, hilog } from '@kit.PerformanceAnalysisKit';
29   ```
30
312. In the **entry/src/main/ets/entryability/EntryAbility.ets** file, set the custom parameters in **onCreate()**. The sample code is as follows:
32
33   ```ts
34    // Assign a value to params, which is a key-value pair
35    let params: Record<string, hiAppEvent.ParamType> = {
36      "test_data": 100,
37    };
38    // Set custom parameters for the crash event.
39    hiAppEvent.setEventParam(params, hiAppEvent.domain.OS, hiAppEvent.event.APP_CRASH).then(() => {
40      hilog.info(0x0000, 'testTag', `HiAppEvent success to set svent param`);
41    }).catch((err: BusinessError) => {
42      hilog.error(0x0000, 'testTag', `HiAppEvent code: ${err.code}, message: ${err.message}`);
43    });
44   ```
45
463. In the **entry/src/main/ets/entryability/EntryAbility.ets** file, add a watcher in **onCreate()** to subscribe to system events. The sample code is as follows:
47
48   ```ts
49    hiAppEvent.addWatcher({
50      // Set the watcher name. The system identifies different watchers based on their names.
51      name: "watcher",
52      // Add the system events to watch, for example, crash events.
53      appEventFilters: [
54        {
55          domain: hiAppEvent.domain.OS,
56          names: [hiAppEvent.event.APP_CRASH]
57        }
58      ],
59      // Implement a callback for the registered system event so that you can apply custom processing to the event data obtained.
60      onReceive: (domain: string, appEventGroups: Array<hiAppEvent.AppEventGroup>) => {
61        hilog.info(0x0000, 'testTag', `HiAppEvent onReceive: domain=${domain}`);
62        for (const eventGroup of appEventGroups) {
63          // The event name uniquely identifies a system event.
64          hilog.info(0x0000, 'testTag', `HiAppEvent eventName=${eventGroup.name}`);
65          for (const eventInfo of eventGroup.appEventInfos) {
66            // Apply custom processing to the event data obtained, for example, print the event data in the log.
67            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.domain=${eventInfo.domain}`);
68            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.name=${eventInfo.name}`);
69            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.eventType=${eventInfo.eventType}`);
70            // Obtain the timestamp of the crash event.
71            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.time=${eventInfo.params['time']}`);
72            // Obtain the type of the crash event.
73            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.crash_type=${eventInfo.params['crash_type']}`);
74            // Obtain the foreground and background status of the crashed application.
75            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.foreground=${eventInfo.params['foreground']}`);
76            // Obtain the version information of the crashed application.
77            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.bundle_version=${eventInfo.params['bundle_version']}`);
78            // Obtain the bundle name of the crashed application.
79            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.bundle_name=${eventInfo.params['bundle_name']}`);
80            // Obtain the process ID of the crashed application.
81            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.pid=${eventInfo.params['pid']}`);
82            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.uid=${eventInfo.params['uid']}`);
83            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.uuid=${eventInfo.params['uuid']}`);
84            // Obtain the exception type, cause, and call stack of the crash event.
85            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.exception=${JSON.stringify(eventInfo.params['exception'])}`);
86            // Obtain the log information about the crash event.
87            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.hilog.size=${eventInfo.params['hilog'].length}`);
88            // Obtain the error log file about the crash event.
89            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.external_log=${JSON.stringify(eventInfo.params['external_log'])}`);
90            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.log_over_limit=${eventInfo.params['log_over_limit']}`);
91            // Obtain the custom test_data of the crash event.
92            hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.test_data=${eventInfo.params['test_data']}`);
93          }
94        }
95      }
96    });
97   ```
98
994. In the **entry/src/main/ets/pages/index.ets** file, add the **appCrash** button and construct a scenario for triggering a crash event in **onClick()**. The sample code is as follows: 
100
101   ```ts
102    Button("appCrash").onClick(()=>{
103      // Construct a scenario in onClick() to trigger a crash event.
104      let result: object = JSON.parse("");
105    })
106   ```
107
1085. In DevEco Studio, click the **Run** button to run the project. Then, click the **appCrash** button to trigger a crash event. 
109
1106. The application crashes. After restarting the application, you can view the following event information in the **Log** window.
111
112   ```text
113   HiAppEvent onReceive: domain=OS
114   HiAppEvent eventName=APP_CRASH
115   HiAppEvent eventInfo.domain=OS
116   HiAppEvent eventInfo.name=APP_CRASH
117   HiAppEvent eventInfo.eventType=1
118   HiAppEvent eventInfo.params.time=1711440614001
119   HiAppEvent eventInfo.params.crash_type=JsError
120   HiAppEvent eventInfo.params.foreground=true
121   HiAppEvent eventInfo.params.bundle_version=1.0.0
122   HiAppEvent eventInfo.params.bundle_name=com.example.myapplication
123   HiAppEvent eventInfo.params.pid=2043
124   HiAppEvent eventInfo.params.uid=20010043
125   HiAppEvent eventInfo.params.uuid=b1e953ba0022c112e4502e28e8b3ad6d95cf3c87bae74068038f03b38ce7f66a
126   HiAppEvent eventInfo.params.exception={"message":"Unexpected Text in JSON","name":"SyntaxError","stack":"at anonymous (entry/src/main/ets/pages/Index.ets:55:34)"}
127   HiAppEvent eventInfo.params.hilog.size=90
128   HiAppEvent eventInfo.params.external_log=["/data/storage/el2/log/hiappevent/APP_CRASH_1711440614112_2043.log"]
129   HiAppEvent eventInfo.params.log_over_limit=false
130   HiAppEvent eventInfo.params.test_data=100
131   ```
132