c# - 查询事件日志时如何创建EventBookmark

标签 c# event-log system.diagnostics

我有一个 EventLogReader 对象,以及事件日志中的一个查询,如下所示:

string query = "*[System[(Level=2) and TimeCreated[@SystemTime>='%LastRun%']]]")

该代码基本上使用读取器来查询自上次运行读取器以来与搜索查询匹配的所有事件。

我宁愿使用 EventBookmark 来实现此目的。毕竟,这就是它的用途!但我无法找到任何工作代码。

我现有的代码部分运行如下:

// This line replaces the %LastRun% code with the date
var myQuery = myEventLogQuery.Query.Replace("%LastRun%", myEventLogQuery.LastRun.ToString("o"));
var query = new EventLogQuery(myEventLogQuery.Log, myEventLogQuery.PathType, myQuery);

// Now set the LastRun date. I want to avoid this...
myEventLogQuery.LastRun = DateTime.UtcNow;

// ... by making this next line smarter.
var reader = new EventLogReader(query);
// var reader = new EventLogReader(query, ??? new EventBookmark());

EventRecord eventRecord;
while ((eventRecord = reader.ReadEvent()) != null)
{
    EventRecords.Add(new EventRecordItem(eventRecord));
}

我应该能够使用第一个(或最后一个)EventRecord 的 EventBookmark 属性来限制下一个查询,但我想将 EventBookmark 最初设置为基本上是日志中的最高记录。

当应用程序最初运行时,我不需要它告诉我过去的所有事件日志条目,我只关心应用程序启动后发生的事件日志条目。

最佳答案

好吧,我继续进行了很多实验,并设法为此制定了一个很好的系统。由于这个主题还没有真正涵盖,我在这里发布我的答案。我希望它有用!

第一个挑战是创建初始 EventBookmark。你不能只实例化一个。您需要从现有的 EventRecord 派生一个。为此,我查询日志中的最后一项,并以此为基础创建我的 EventBookmark。

using System.Diagnostics.Eventing.Reader;
public class MyEventLogQuery
{
    public string Log { get; set; }
    public PathType PathType { get; set; }
    public string Query { get; set; }
    public EventBookmark Bookmark { get; set; }

    public MyEventLogQuery(string log = "Application", PathType pathType = PathType.LogName, string query = "*[System[(Level=2)]]")
    {
        Log = log;
        PathType = pathType;
        Query = query;
        Bookmark = GetBookmark(log, pathType); // Query is not important here
    }

    // This method returns the bookmark of the most recent event
    // log EventRecord or null if the log is empty
    private static EventBookmark GetBookmark(string log, PathType pathType)
    {
        var elq = new EventLogQuery(log, pathType) {ReverseDirection = true};

        var reader = new EventLogReader(elq);
        var record = reader.ReadEvent();
        if (record != null)
            return record.Bookmark;
        return null;
    }
}

下一步是在随后查询事件日志时使用书签(或缺少书签)。

using System.Diagnostics.Eventing.Reader;
public class EventLogTracker()
{
    public List<MyEventLogQuery> Queries { get; set; }

    // ... snipped some stuff
    public int Run()
    {
        var count = 0;
        foreach (var myQuery in Queries)
        {
            var query = new EventLogQuery(myQuery.Log, myQuery.PathType, myQuery.Query);

            // This is the important bit. Must take account that the
            // log may have been empty, so bookmark could be null
            var reader = myQuery.Bookmark != null ? new EventLogReader(query, myQuery.Bookmark) : new EventLogReader(query);
            EventRecord eventRecord;
            while ((eventRecord = reader.ReadEvent()) != null)
            {
                // Do stuff
                // ...
                // Then update the bookmark
                myQuery.Bookmark = eventRecord.Bookmark;
                count++;
            }
        }
        return count;
    }

瞧,您的代码使用 EventBookmark 仅向您提供自应用程序启动以来发生的事件。

关于c# - 查询事件日志时如何创建EventBookmark,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31509513/

相关文章:

c# - 对明显非空对象的空引用

c# - 我需要线程锁对象吗?

windows - 事件 ID 与事件查看器中显示的内容不匹配

macos - Mac OS X 盖子打开/关闭事件日志

c# - 如何为 HttpClient 调用配置网络跟踪 Dotnet 核心?

c# - C# 有办法给我一个不可变的字典吗?

c# - 实体名称枚举

.net - 在WiX中创建EventLog源,而没有事件消息文件

c# - 如何在网站的进程结束时显示消息?

c# - 检索按 Alt-Tab 列表中的顺序打开的窗口的顺序?