[英]Is there a built-in method to binary search through the entries in the .NET EventLog.Entries collection?
我正在開發一種日志解析服務,該服務可以捕獲Windows事件日志中的特定安全事件。 我最初的想法是使用Microsoft的LogParser ,但是除了選擇預先已知的特定實例/事件ID外,我沒有尋找任何功能。
經過一些基准測試后,我發現遍歷整個.NET EventLog.Entries
集合在提取數據上的速度比查詢Microsoft的LogParser快3倍以上。
最終,要提取的數據將保存在SQL Server數據庫中。 由於該服務每天將執行此任務,因此我希望避免重復的條目,並且需要一種方法來在EventLog.Entries
集合中找到數據庫中尚未存在的下一個條目。 找到初始條目后,我就可以開始插入數據庫了。
我將要編寫一個二進制搜索來使用數據庫中的最新DATETIME
時間戳字段查找該條目,並將其與EventLog.Entries
集合中某個項目的TimeWritten
屬性進行比較。 我可以這樣做,但是我想知道是否已經有內置方法可以執行此搜索?
我最終寫了我自己的,因為找不到內置的實現:
/// <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;
}
我不了解EventLogEntryCollection
,但是如果您需要通用的二進制搜索算法,則可以使用PowerCollections庫中實現的算法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.