阅读最新的事件日志详情

时间:2018-03-11 15:01:12

标签: c# printing event-log

所以我一直在通过事件日志监视我的打印作业并且它运行良好,现在我想获得它正在打印的副本数量,但问题是它读取最旧的事件日志而不是最新的事件日志。

private void checkCopies()
    {
        EventLogQuery logQuery = new EventLogQuery("Microsoft-Windows-PrintService/Operational", PathType.LogName, "*[System[(EventID = 805)]]");
        EventLogWatcher logWatcher = new EventLogWatcher(logQuery);
        EventLogReader logReader = new EventLogReader(logQuery);
        logWatcher.EventRecordWritten += new EventHandler<EventRecordWrittenEventArgs>(getCopies);
        logWatcher.Enabled = true;
    }
private void getCopies(Object obj, EventRecordWrittenEventArgs arg)
    {
        EventLogQuery logQuery = new EventLogQuery("Microsoft-Windows-PrintService/Operational", PathType.LogName, "*[System[(EventID = 805)]]");
        EventLogReader logReader = new EventLogReader(logQuery);
        EventRecord rec = logReader.ReadEvent();
        if (rec != null)
            totalCopies = rec.ToXml().ToString();
    }
private void getPrintedCopies()
    {
        Regex parts = new Regex(@"<Copies>(\d+)</Copies>");
        Match match = parts.Match(totalCopies);
        if (match.Success)
            copies = Convert.ToInt32(match.Groups[1].Value);
    }

我该怎么做才能让它读取最新的日志呢?

1 个答案:

答案 0 :(得分:0)

好的,我通过清除PrintService的事件日志找到了某种解决方案,可以在这里找到:

How do I programmatically or regularly clear the Operational PrintService event log?

现在,我可以获得打印作业监控应用的副本数量和页数。