我正在查询不同域控制器的事件日志,我必须在一段时间后继续查询。
下面是我用来查询它的代码。
public static void FindAllLog(string machineName)
{
//EventLog log = new EventLog("", "");
//log.
EventLog[] remoteEventLogs;
// Gets logs on the local computer, gives remote computer name to get the logs on the remote computer.
remoteEventLogs = EventLog.GetEventLogs(machineName);
Console.WriteLine("Number of logs on computer: " + remoteEventLogs.Length);
for (int i = 0; i < remoteEventLogs.Length; i++)
{
Console.WriteLine("Log : " + remoteEventLogs[i].Log);
ReadEventLog(machineName, remoteEventLogs[i].Log, DateTime.Now.AddDays(-30));
//ReadAppEventLog(machineName, remoteEventLogs[i].Log);
}
}
public static void ReadEventLog(string machine, string logType,DateTime fromDate)
{
EventLog ev = new EventLog(logType, machine);
var entry = (from EventLogEntry e in ev.Entries
where e.TimeGenerated >= fromDate
orderby e.TimeGenerated
select e);//.LastOrDefault();
foreach (EventLogEntry CurrentEntry in entry)
{
Console.WriteLine("Event ID : " + CurrentEntry.EventID);
Console.WriteLine("Event Source : " + CurrentEntry.Source);
Console.WriteLine("Event TimeGenerated : " + CurrentEntry.TimeGenerated);
Console.WriteLine("Event TimeWritten : " + CurrentEntry.TimeWritten);
Console.WriteLine("Event MachineName : " + CurrentEntry.MachineName);
Console.WriteLine("Entry Type : " + CurrentEntry.EntryType.ToString());
Console.WriteLine("Message : " + CurrentEntry.Message + "\n");
Console.WriteLine("-----------------------------------------");
}
}
当我第一次查询域控制器时,我必须阅读过去 30 天的日志。否则只需阅读我们上次离开时的最新日志。查询它要花很多时间吗?我尝试使用 WMI,同样的问题地狱很多时间,它有时会给出“无效的查询错误”?如何改善这一点?您建议执行此任务的任何模型,我在这里为每个域控制器执行多线程?
谢谢