【问题标题】:is the event log index unique事件日志索引是否唯一
【发布时间】:2013-01-29 20:20:15
【问题描述】:

我通过以下方式从 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++;
  }
}

【问题讨论】:

    标签: c# event-viewer


    【解决方案1】:

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

    但是你错过了几件事。

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

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

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

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

    然后将oldestIndex 与您的lastReadedIndex 进行比较,如果lastReadedIndex &lt; oldestIndex 您首先必须读取存档,并且只读取当前事件日志文件。

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

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

    祝你好运!

    【讨论】:

    • 是的,我使用了上次检查日期。我会为那些将来搜索我所做的事情的人更新我的帖子。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 2015-02-04
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 2022-05-11
    相关资源
    最近更新 更多