ReadHistoryEvents
如果服务端记录事件历史, 客户端可以按时间区间拉历史事件.
配套
- 实时事件订阅请用 事件订阅.
- 历史变量数据请用 ReadHistory (Raw) / ReadHistoryProcessed.
签名
std::vector<EventArrivedEventArgs> ReadHistoryEvents(
std::string const& node_id, // 通常 i=2253 Server
int64_t start_ft, int64_t end_ft,
uint32_t max_events = 0);
EventArrivedEventArgs
struct EventArrivedEventArgs {
uint16_t severity = 0; // 1-1000
std::string message;
std::string source_name;
int64_t time = 0; // FileTime (UTC 100ns since 1601)
std::string event_type;
};
服务端返回的每条事件已经在 SDK 层抽取成扁平结构, 默认拉常用字段 (Severity / Message / SourceName / Time / EventType).
用法
constexpr int64_t WIN_EPOCH_OFFSET = 116444736000000000LL;
auto now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
int64_t end_ft = WIN_EPOCH_OFFSET + now_ns / 100;
int64_t start_ft = end_ft - 24LL * 3600 * 10000000;
auto events = s.ReadHistoryEvents("i=2253", start_ft, end_ft);
for (auto const& e : events) {
std::cout << "[" << e.severity << "] " << e.source_name
<< ": " << e.message
<< " ft=" << e.time
<< " type=" << e.event_type << "\n";
}
自定义 EventFilter
如果想过滤 EventType / Severity, 后续 SDK 版本将提供 EventFilter builder API. 当前默认拉 BaseEventType (全部事件).
服务端要求
- 必须支持 HistoryRead Events (服务端 ServerCapabilities 标志位)
- 事件源节点必须有 EventNotifier Attribute (12) bit 1 (HistoryRead) 置位
与实时事件订阅的关系
| API | 拿什么 |
|---|---|
s.SubscribeEvents(...) | 实时事件 (订阅期间发生的) |
s.ReadHistoryEvents(...) | 历史事件 (服务端归档的) |
通常组合用: 启动时 ReadHistoryEvents 拉过去 24 小时, 然后 SubscribeEvents 接管实时.