繁体   English   中英

事件日志索引是唯一的

[英]is the event log index unique

我正在从Windows中的事件日志中获取以下信息:

private void button1_Click(object sender, EventArgs e)
{
  EventLog eventLog;
  eventLog = new EventLog();
  eventLog.Log = "Security";;
  eventLog.Source = "Security-Auditing";
  eventLog.MachineName = "SERVER";

  var count = 0;
  foreach (EventLogEntry log in eventLog.Entries.Cast<EventLogEntry>().Where(log => log.InstanceId == 4625))
  {
    Console.Write("eventLogEntry.Index: {0}{1}", log.Index, Environment.NewLine);
    SaveRecord(log);
    count++;
  }
}

我试图捕获所有无效登录到我的服务器,然后在x次无效尝试之后添加一个条目。

我正在遍历事件日志并毫无问题地获取信息,但是我怎么知道我停止读取的最后一条记录是什么? 当日志获取更多信息时,我需要重新开始读取,但需要一个起点。

我以为可以在EventLogEntry上使用索引,但是找不到任何信息。 我的机器上有6位数字。

那有多可靠? 我应该去别的东西吗? 我应该在阅读日志后清除该日志吗?

感谢您的输入!

=======我要做什么========

在Apokal的帮助下,这是我所做的:

/// <summary>
/// Returns all events in the windows event log that match the passed in criteria.
/// If nothing is passed in then it will return all events where the a user attemtped
/// to log into the the machine and gave an invalid username or password.
/// </summary>
/// <param name="eventLogMachineName">The machine where the event log is at.</param>
/// <param name="cutoffdatetime">Date and time of the cut off for the list.</param>
/// <param name="eventLogName">'Log Name' in the event log. This is the folder that the events reside in.</param>
/// <param name="eventLogSource">Event log 'Source'.</param>
/// <param name="instanceId">Filters to a specific 'Event Id' in the event log.</param>
/// <returns></returns>
public static IEnumerable<EventLogEntry> GetEventLogs(string eventLogMachineName,
                                                      DateTime cutoffdatetime,
                                                      string eventLogName = "Security",
                                                      string eventLogSource = "Security-Auditing",
                                                      int instanceId = 4625)
{
  var eventLog = new EventLog {Log = eventLogName, Source = eventLogSource, MachineName = eventLogMachineName};
  return from EventLogEntry log in eventLog.Entries
         where log.InstanceId == instanceId &&
               log.TimeGenerated > cutoffdatetime
         select log;
}

我这样称呼它:

private void button1_Click(object sender, EventArgs e)
{
  var lastcheckdatetime = Properties.Settings.Default.LastCheckDate;
  if (lastcheckdatetime < (DateTime.Now.AddDays(-30)))
  {
    lastcheckdatetime = DateTime.Now.AddDays(-7);
  }
  var log = EventLogClass.GetEventLogs("TGSERVER", lastcheckdatetime);
  Properties.Settings.Default.LastCheckDate = DateTime.Now;
  Properties.Settings.Default.Save();

  var count = 0;
  foreach (EventLogEntry l in log)
  {
    Console.WriteLine("---------------------");
    Console.Write("eventLogEntry.Index: {0}{1}", l.Index, Environment.NewLine);
    Console.Write("eventLogEntry.TimeGenerated: {0}{1}", l.TimeGenerated, Environment.NewLine);
    count++;
  }
}

根据我的经验和很少的研究, Index属性显示了从创建事件日志开始编写的事件索引。

但是您错过了几件事。

首先,您必须记住事件日志的大小有限。 例如,假设“安全性”日志只能容纳1000个条目(如果查看eventvwr.msc,则eventlog属性中显示的实际大小(以mb为单位))。 因此,事件日志已满时,有3种方法:

  1. 在旧事件上写下新事件。 在这种情况下,记住最后读取的事件索引是不好的。 因为该索引指向的事件可以简单地覆盖。
  2. 存档。 在这种情况下,记住的索引现在可以指向存档中的事件,而不是事件日志的当前.evtx文件中的事件
  3. 不要编写新事件,请手动清除事件日志。 我认为这并不有趣,因为您需要一个自动化工具。

因此,可以将事件日志设置为存档并记住事件的最后索引。 然后,当再次读取事件日志时,首先获取当前事件日志文件的最早记录:

EventLog log = new System.Diagnostics.EventLog("Security");
int oldestIndex = log.Entries[(int)eli.OldestRecordNumber].Index;

然后比较oldestIndex与你lastReadedIndex如果lastReadedIndex < oldestIndex你首先要读取档案,只比读取当前的事件日志文件。

默认情况下,所有档案都存储在当前事件日志文件(.evtx)存在的同一目录中。 使用EventLogReader类可以轻松读取档案。 尝试查看EventRecord及其RecordId属性,我认为它与EventLogEntry类的Index属性相同(目前无法检查)。

另一种方法是记住事件的编写时间,并将其用作搜索新事件的起点,以防IndexRecordId

祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM