• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Introduction to HiAppEvent
2
3<!--Kit: Performance Analysis Kit-->
4<!--Subsystem: HiviewDFX-->
5<!--Owner: @liujiaxing2024-->
6<!--Designer: @junjie_shi-->
7<!--Tester: @gcw_KuLfPSbe-->
8<!--Adviser: @foryourself-->
9
10## Overview
11
12HiAppEvent is an event logging mechanism provided for recording faults, statistics, security events, and behavior events during application running. It helps you locate faults, analyze application running status, and collect statistics on access traffic, user activity, operation habits, and other key factors that affect user experience.
13
14## Basic Concepts
15
16**Logging**: a function that logs changes caused by user operations to provide service data for development, product, and O&M analysis.
17
18- Event domain: identifies the domain of an event. You are advised to set this parameter to the service domain name to differentiate service domains.
19
20- Event name: specifies the name of an event. You are advised to set this parameter to a specific service name to differentiate services.
21
22- Event type: specifies the type of an event. Four event types are supported:
23
24  - Behavior event: used to record the daily operation behavior of a user, for example, button click and page redirection.
25  - Fault event: used to locate and analyze application faults, for example, frame freezes and network interruption.
26  - Statistical event: used to collect statistics on key application behaviors, for example, usage duration and number of visits.
27  - Security event: used to record events related to application security, for example, user authorization.
28
29- Event parameter: specifies the parameters of an event. Each event can contain a group of parameters. You are advised to set this parameter to an event attribute or event context to depict the event details.
30
31**Event subscription**: By calling the **addWatcher()** API of the HiAppEvent, you can register a watcher for system or application events as required. In this way, when the subscribed event occurs, you can receive and process the event callback information.
32
33## Implementation Principles
34
35### System Event Subscription Mechanism
36
37In the current system application sandbox mechanism, an application process can access only its own application sandbox directory. For details, see [Application Sandbox](../file-management/app-sandbox-directory.md). However, the system event information is not stored in the application sandbox directory. As a result, the information cannot be directly obtained.
38
39The application calls the **addWatcher()** API of HiAppEvent to subscribe to system events and create a shared directory. When the application process is faulty, the DFX system captures the related information, generates events and logs, and shares them to the shared directory. After detecting an event, HiAppEvent sends the event to the application.
40
41![hiappevent-watch-systemevents](figures/hiappevent-watch-systemevents.PNG)
42
43### Application Event Subscription Mechanism
44
45After an application calls the **addWatcher()** API to subscribe to the application events, it must call the **write()** API to record the application events when they occur.
46
47HiAppEvent associates application events based on the event domain and event name, and sends the events to the application using the callback set through the **addWatcher()** API.
48
49![hiappevent-watch-appevents](figures/hiappevent-watch-appevents.PNG)
50
51## Constraints
52
53- The **addWatcher()** API is a synchronous API and involves I/O operations. For modules that have high requirements on performance, you are advised to call the API on a non-main thread.
54
55- The name passed to the **addWatcher()** API should be unique. If the same name is passed, the previous subscription will be overwritten.
56