c# - 优化从 System.Diagnostics.EventLog 读取的 LINQ
问题描述
我在使用以下查询的某些计算机上遇到性能问题:
System.Diagnostics.EventLog log = new System.Diagnostics.EventLog("Application");
var entries = log.Entries
.Cast<System.Diagnostics.EventLogEntry>()
.Where(x => x.EntryType == System.Diagnostics.EventLogEntryType.Error)
.OrderByDescending(x => x.TimeGenerated)
.Take(cutoff)
.Select(x => new
{
x.Index,
x.TimeGenerated,
x.EntryType,
x.Source,
x.InstanceId,
x.Message
}).ToList();
显然ToList()
在某些查询中可能会很慢,但我应该用什么替换它?
解决方案
log.Entries
集合的工作方式是这样的:它知道事件的总数(log.Entries.Count
)以及当您访问单个元素时 - 它会进行查询以获取该元素。
这意味着当您枚举整个Entries
集合时 - 它会查询每个单独的元素,因此会有Count
查询。并且您的 LINQ 查询的结构(例如,OrderBy
)强制对该集合进行完整枚举。正如您已经知道的那样 - 这是非常低效的。
更有效的可能是只查询您需要的日志条目。为此,您可以使用EventLogQuery
类。假设您有一个简单的类来保存事件信息详细信息:
private class EventLogInfo {
public int Id { get; set; }
public string Source { get; set; }
public string Message { get; set; }
public DateTime? Timestamp { get; set; }
}
然后您可以像这样转换低效的 LINQ 查询:
// query Application log, only entries with Level = 2 (that's error)
var query = new EventLogQuery("Application", PathType.LogName, "*[System/Level=2]");
// reverse default sort, by default it sorts oldest first
// but we need newest first (OrderByDescending(x => x.TimeGenerated)
query.ReverseDirection = true;
var events = new List<EventLogInfo>();
// analog of Take
int cutoff = 100;
using (var reader = new EventLogReader(query)) {
while (true) {
using (var next = reader.ReadEvent()) {
if (next == null)
// we are done, no more events
break;
events.Add(new EventLogInfo {
Id = next.Id,
Source = next.ProviderName,
Timestamp = next.TimeCreated,
Message = next.FormatDescription()
});
cutoff--;
if (cutoff == 0)
// we are done, took as much as we need
break;
}
}
}
它将快 10-100 倍。但是,此 API 更底层并返回EventRecord
(而不是EventLogEntry
)的实例,因此对于某些信息,可能有不同的方式来获取它(与 相比EventLogEntry
)。
如果您决定绝对必须使用log.Entries
and EventLogEntry
,那么至少Entries
向后枚举。那是因为最新的事件在最后(它按时间戳升序排序),并且您需要按时间戳降序排列的前 X 个错误。
EventLog log = new System.Diagnostics.EventLog("Application");
int cutoff = 100;
var events = new List<EventLogEntry>();
for (int i = log.Entries.Count - 1; i >= 0; i--) {
// note that line below might throw ArgumentException
// if, for example, entries were deleted in the middle
// of our loop. That's rare condition, but robust code should handle it
var next = log.Entries[i];
if (next.EntryType == EventLogEntryType.Error) {
// add what you need here
events.Add(next);
// got as much as we need, break
if (events.Count == cutoff)
break;
}
}
这效率较低,但仍应比您当前的方法快 10 倍。请注意,它更快,因为Entries
集合没有在内存中实现。当您访问单个元素时会查询它们,并且在您的特定情况下向后枚举时 - 很有可能查询更少的元素。
推荐阅读
- flutter - Flutter 中的白平衡/自动对焦
- c++ - g ++包含路径被忽略?
- json - 在python中使用for循环迭代嵌套字典
- python - 如果包含某个单词,如何列出文件列表,但如果包含其他单词,则使用 glob python 排除?
- python - 无法打印发送到生成器的值
- node.js - 如何将 node-fetch 与 Cloud Functions for Firebase 结合使用。无法部署
- javascript - 语义UI React中的React Router Navigate问题
- android - 如何在 SpannableString 中查找子字符串的索引
- aix - 在 AIX 上测试 shell 脚本
- python-3.x - 运行 PyMC3 模型时如何解决内存错误