这个问题已经在这里有一个答案:
- 什么是读取远程计算机上的事件日志的最快方法? 7个回答
我是不同的域控制器的查询事件日志,我必须不断询问一些时间间隔之后。
下面是我使用的查询它的代码。
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,同样的问题地狱大量的时间和它有时给人“无效查询错误”? 如何提高呢? 你认为做这个任务的任何模式,我做的多线程这里对每个域控制器?
谢谢