【问题标题】:GetNumberOfEventLogRecords returns incorrect number of event logsGetNumberOfEventLogRecords 返回错误的事件日志数
【发布时间】:2015-10-16 05:07:58
【问题描述】:

我有这个 C++ 代码来读取事件日志记录

DWORD GetLogRecords(LPCWSTR wsLogFile)
{
  HANDLE hEvt = OpenEventLog(NULL, wsLogFile);
  if (hEvt==NULL) return 0;

  DWORD dwTotalRecords;
  BOOL res = GetNumberOfEventLogRecords(hEvt, &dwTotalRecords);
  CloseEventLog(hEvt);

  return (res != 0) ? dwTotalRecords : 0;
}

结果

atlTraceGeneral - C:\Windows\system32\winevt\logs\ACEEventLog.evtx - 23499 Total Records
atlTraceGeneral - C:\Windows\system32\winevt\logs\Application.evtx - 23499 Total Records
atlTraceGeneral - C:\Windows\system32\winevt\logs\ConnectionInfo.evtx - 23499 Total Records
atlTraceGeneral - C:\Windows\system32\winevt\logs\Error.evtx - 23499 Total Records
atlTraceGeneral - C:\Windows\system32\winevt\logs\HardwareEvents.evtx - 23499 Total Records
atlTraceGeneral - C:\Windows\system32\winevt\logs\Internet Explorer.evtx - 23499 Total Records
atlTraceGeneral - C:\Windows\system32\winevt\logs\Key Management Service.evtx - 23499 Total Records
 ...

我已使用计算机上所有 .EVTX 日志文件的完整路径(150 个日志文件)调用此函数。并且每次返回 23499 !我的日志文件有不​​同的大小和一些 0,为什么我总是得到 23499?

UPDATE2:现在我清除了应用程序日志后,所有 .evtx 日志文件都为 0。我认为它总是获取应用程序日志而不是指定的 .evtx 文件。

更新:正如 Remy Lebeau 所建议的,但结果仍然相同。

【问题讨论】:

  • 如果您指定自定义日志但无法找到,则事件日志服务会打开应用程序日志,因此您的答案就在于此,问题一定出在您没有的代码中'没有给我们看。
  • 代码在这里,.evtx文件路径是正确的。
  • 您是否尝试过运行上述函数并指定一个 .evtx 日志文件并且成功了?
  • 基于示例here 我猜问题是您指定了完整路径,而您并不打算这样做。请注意,他们使用字符串 "MyEventProvider" 调用 OpenEventLog - 没有路径,甚至没有文件扩展名。
  • 试一试...(例如"HardwareEvents" 而不是"c:\\windows\\....\\HardwareEvents.evtx"

标签: c++ windows winapi service event-log


【解决方案1】:

您没有检查GetNumberOfEventLogRecords() 的结果是否有错误。而且您正在泄漏日志句柄。试试这个:

DWORD GetLogRecords(LPCWSTR wsLogFile)
{
  HANDLE hEvt = OpenEventLog(NULL, wsLogFile);
  if (hEvt==NULL) return 0;

  DWORD dwTotalRecords;
  BOOL res = GetNumberOfEventLogRecords(hEvt, &dwTotalRecords);
  CloseEventLog(hEvt);

  return (res != 0) ? dwTotalRecords : 0;
}

【讨论】:

  • 没有帮助,我现在仍然在所有 .EVTX 日志文件中获得 23498 条记录。
  • 您是否调试了代码以验证 GetLogRecord() 实际上返回 23498,并且当您在跟踪消息中输出它时不只是破坏了该值?
  • 是的,我做到了。我认为每次读取应用程序日志时都会如此,因为我已经清除了应用程序日志,现在所有日志都为 0。
【解决方案2】:

为了他人的利益,这个问题的解决方案是OpenEventLog 不接受路径名。相反,您必须为其提供事件日志的源名称(类似于"HardwareEvents")。

如果您使用无效的源名称(包括提供路径名)调用 OpenEventLog,则如记录所示,它将打开 Application 日志:

如果您指定了自定义日志但找不到,则事件日志记录 服务打开应用程序日志。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 2011-11-01
    • 2010-09-06
    • 1970-01-01
    相关资源
    最近更新 更多