【问题标题】:Reading Applications and services log阅读应用程序和服务日志
【发布时间】:2021-08-07 16:30:44
【问题描述】:

我想读取存储在 Windows 事件日志的应用程序和服务日志部分下的自定义事件日志。

不幸的是,根据其命名属性调用日志时,我收到一条错误消息,提示找不到日志。

我尝试从具有特定 ID 的事件中读取事件详细信息,但首先我需要能够访问日志。

这是我目前的代码:

Imports System
Imports System.Diagnostics.Eventing.Reader

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim query As New EventLog("Logname as per Properties", System.Environment.MachineName)
        Dim elEventEntry As System.Diagnostics.EventLogEntry

        Dim nmbr As Integer = query.Entries.Count

        MsgBox(nmbr)

    End Sub
End Class

这是事件日志中的结构(我想阅读蓝色突出显示的部分)

有人知道如何确定正确的日志名称吗?

谢谢 & BR 丹尼尔

【问题讨论】:

  • 这能回答你的问题吗? How to retrieve event log other than Application category? 此外,How to: Access and Read Event Information 可能很有用,尽管 VB.NET 代码示例尚未完全转换为 VB.NET。
  • (查询将是 *[System/EventID=1] 或您需要的任何其他事件 ID。)
  • @AndrewMorton 非常感谢您提供的信息!尽管进行了搜索,但它没有提供该信息。我首先需要使用 EventLogReader 打开事件日志,然后使用 EventLogQuery 进行过滤,这是否正确?

标签: vb.net operating-system event-log


【解决方案1】:

对于许多事件日志,您需要使用EventLogQuery。

例如,如果您想查询“设置”事件日志以计算 EventID 为 1 的条目数,您可以这样做:

Imports System.Diagnostics.Eventing.Reader

Module Module1

    Sub Main()
        Dim query As New EventLogQuery("Setup", PathType.LogName, "*[System/EventID=1]")
        Dim nEvents = 0

        Using logReader = New EventLogReader(query)
            Dim eventInstance As EventRecord = logReader.ReadEvent()
            While Not eventInstance Is Nothing
                nEvents += 1
                eventInstance = logReader.ReadEvent()
            End While

        End Using

        Console.WriteLine(nEvents)

        Console.ReadLine()

    End Sub

End Module

您可以通过在 Windows 事件查看器中查看事件的 XML 来查看要查询的项目的名称。

Using 构造确保EventLogReader 在使用后得到妥善处理。

更多信息:How to: Access and Read Event Information (from Microsoft)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多