是否有内置方法可以通过 .NET EventLog.Entries 集合中的条目进行二分搜索?

发布于 2024-07-18 01:49:41 字数 694 浏览 11 评论 0原文

我正在开发一个日志解析服务,用于捕获 Windows 事件日志中的特定安全事件。 我最初的想法是使用微软的 LogParser,但除了选择预先已知的特定实例/事件 ID 之外,我并不寻找任何功能。

经过一些基准测试后,我发现迭代整个 .NET EventLog.Entries 集合提取数据的速度比查询 Microsoft LogParser 的速度快 3 倍以上。

最终,要拉取的数据将保存在 SQL Server 数据库中。 由于该服务每天都会执行此职责,因此我希望避免重复条目,并且我需要一种方法来查找 EventLog.Entries 集合中尚未存在于数据库中的下一个条目。 一旦找到初始条目,我就可以开始插入数据库。

我正准备使用数据库中最新的 DATETIME 时间戳字段编写二分搜索来查找此条目,并将其与 TimeWritten 中的项目的属性进行比较code>EventLog.Entries 集合。 我可以做到这一点,但我想知道是否已经有一个内置方法来执行此搜索?

I am developing a log parsing service that captures specific security events in the Windows Event Log. My initial thought was to use Microsoft's LogParser, but I am not looking for any functionality beyond selecting specific Instance/Event IDs already known in advance.

After some benchmarking, I found that iterating over the entire .NET EventLog.Entries collection was over 3 times faster at pulling data than querying Microsoft's LogParser.

Ultimately, the data to be pulled will be saved in a SQL Server database. Since the service will perform this duty daily, I wish to avoid duplicate entries, and I will need a way to find the next entry in the EventLog.Entries collection that is not already in the database. I can begin inserting to the database once I've found that initial entry.

I was just about to write a binary search to find this entry using the most recent DATETIME timestamp field from the database and comparing it to the TimeWritten property from an item in the EventLog.Entries collection. This I can do, but I am wondering if there is already a built-in method to perform this search?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎言笑 2024-07-25 01:49:41

由于找不到内置实现,我最终编写了自己的实现:

/// <summary>
/// Performs a binary search on a specified EventLogEntryCollection's
/// TimeWritten property
/// </summary>
/// <param name="entries">The collection to search</param>
/// <param name="value">The timestamp value being searched</param>
/// <param name="low">The lower-bound search index</param>
/// <param name="high">The upper-bound search index</param>
/// <returns>The index of a matching timestamp, or -1 if not found</returns>
private int BinarySearch(EventLogEntryCollection entries, DateTime value, int low, int high)
{
    if (high < low)
        return -1;
    int mid = low + ((high - low) / 2);
    if (entries[mid].TimeWritten > value)
        return BinarySearch(entries, value, low, mid - 1);
    else if (entries[mid].TimeWritten < value)
        return BinarySearch(entries, value, mid + 1, high);
    else
        return mid;
}

I ended up writing my own since I could not find a built-in implementation:

/// <summary>
/// Performs a binary search on a specified EventLogEntryCollection's
/// TimeWritten property
/// </summary>
/// <param name="entries">The collection to search</param>
/// <param name="value">The timestamp value being searched</param>
/// <param name="low">The lower-bound search index</param>
/// <param name="high">The upper-bound search index</param>
/// <returns>The index of a matching timestamp, or -1 if not found</returns>
private int BinarySearch(EventLogEntryCollection entries, DateTime value, int low, int high)
{
    if (high < low)
        return -1;
    int mid = low + ((high - low) / 2);
    if (entries[mid].TimeWritten > value)
        return BinarySearch(entries, value, low, mid - 1);
    else if (entries[mid].TimeWritten < value)
        return BinarySearch(entries, value, mid + 1, high);
    else
        return mid;
}
以酷 2024-07-25 01:49:41

我不知道 EventLogEntryCollection,但如果您需要通用的二分搜索算法,您可以使用 PowerCollections 库。

I don't know about EventLogEntryCollection, but if you need a generic binary search algorithm, you can use the one implemented in PowerCollections library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文