0
我如何在控制檯中讀取特定的Windows安全日誌(例如鎖定事件)。我需要今天的事件。來讀取特定的Windows安全日誌
我如何在控制檯中讀取特定的Windows安全日誌(例如鎖定事件)。我需要今天的事件。來讀取特定的Windows安全日誌
首先,您應該知道源的名稱(這是事件查看器中的列,通常是寫入事件日誌信息的應用程序的名稱)。
添加System.Diagnostics命名空間中引用您的代碼文件:
using System.Diagnostics
然後,你必須創建事件日誌的實例。
// First, check if event log contains required source
if(EventLog.SourceExists("YourSourceName"))
{
// Specify your source name and log name (e.g. Application, System or some custom name)
EventLog log = new EventLog()
{
Source = "YourSourceName",
Log = "Application"
};
// Enumerate through log entries
foreach (EventLogEntry entry in log.Entries)
{
// Do something with log entries
Console.WriteLine(entry.Message);
}
// You also may filter log entries by date (LINQ is used for this)
foreach (EventLogEntry entry in log.Entries.Cast<EventLogEntry>().Where(x => (DateTime.Now - x.TimeGenerated).Days == 0))
{
// Do something with log entries
Console.WriteLine(entry.Message);
}
}
希望這會有所幫助。