首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 3.0 EventLog 寫入 Windows 事件日誌實戰(二):系統日誌、自定義日誌與事件源管理

     編輯:關於C語言

寫入系統日誌

需先創建事件源再寫入:


var log = new EventLog("System");
if (!EventLog.SourceExists("My Application"))
    EventLog.CreateEventSource("My Application", "System");

log.Source = "My Application";
log.WriteEntry("處理信息1", EventLogEntryType.Information);
log.WriteEntry("處理信息2", EventLogEntryType.Information);

try { throw new FileNotFoundException("readme.txt未找到"); }
catch (FileNotFoundException ex)
{
    log.WriteEntry(ex.Message, EventLogEntryType.Error);
}

創建自定義日誌


var log = new EventLog("MyEvent");
if (!EventLog.SourceExists("New Application"))
    EventLog.CreateEventSource("New Application", "MyEvent");

log.Source = "New Applications";
log.WriteEntry("處理信息1", EventLogEntryType.Information);
// ... 同理

關鍵規則

  • 一個事件源同一時間只能綁定一個日誌
  • 寫入非 Application 日誌必須先 CreateEventSource
  • 事件源名稱全局唯一(機器級)
  • 需要管理員權限創建事件源/日誌

事件類型枚舉


EventLogEntryType.Information // 藍色圖標
EventLogEntryType.Warning     // 黃色三角
EventLogEntryType.Error       // 紅色叉
EventLogEntryType.SuccessAudit / FailureAudit // 審計

總結

EventLog 類提供直接寫入 Windows 事件系統的能力。掌握事件源預創建、日誌選擇、類型枚舉、權限要求,即可在應用中實現標準化系統級日誌記錄,便於運維監控與審計追蹤。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved