即時監控 Windows 事件日誌,特定事件觸發郵件/簡訊/系統告警。適用於安全審計、服務健康監控、合規稽核。
/clr:oldSyntax 或遷移新語法。System.Diagnostics.EventLog。#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
__gc class NewLogEntryHandler {
public:
void OnNewLogEntry(Object* sender, EntryWrittenEventArgs* e) {
EventLogEntry* entry = e->Entry;
// 處理邏輯:篩選、告警、寫入資料庫...
Console::WriteLine("New Event: {0} - {1}", entry->Source, entry->Message);
}
};
EventLog* log = new EventLog("Application"); // 或 System, Security, 自訂日誌
log->EnableRaisingEvents = true;
NewLogEntryHandler* handler = new NewLogEntryHandler();
log->EntryWritten += new EntryWrittenEventHandler(handler, &NewLogEntryHandler::OnNewLogEntry);
| 屬性 | 說明 | 篩選範例 |
|---|---|---|
| MachineName | 來源電腦名 | 分布式部署識別節點 |
| Source | 事件來源/應用程式名 | 只關注 "MyService" |
| Message | 人類可讀描述 | 關鍵字匹配 "Error", "Timeout" |
| EntryType | Information/Warning/Error/SuccessAudit/FailureAudit | 僅處理 Error + FailureAudit |
| EventID | 應用程式特定事件編號 | 對應已知故障碼 |
| Data | 二進位資料 (記憶體傾印等) | 深度診斷用 |