系统相关
首页 > 系统相关> > 如何在C#中获取所有Windows事件日志(事件查看器日志)及其层次结构和友好名称的列表

如何在C#中获取所有Windows事件日志(事件查看器日志)及其层次结构和友好名称的列表

作者:互联网

我正在尝试从事件查看器中复制以下内容

enter image description here

我遇到了一些问题.首先,我得到的一些名字不是显示名称或友好名称.例如,对于“Microsoft Office Alerts”,我只是回到“OAlerts”.如何从“OAlerts”获得完整的“Microsoft Office Alerts”?

第二个问题是弄清楚层次结构.似乎所有我能做的就是解析破折号并进行某种最好的猜测.在API中似乎没有一种简单的方法可以解决这个问题. GetLogNames只为您提供所有日志的平面列表

EventLogSession session = new EventLogSession();
List<string> logNames = new List<string>(session.GetLogNames());
foreach (string name in logNames)
{
   //EventLogConfiguration config = new EventLogConfiguration(name); //looks useful but doesn't give me any of the info i'm looking for.

   Console.WriteLine(name);
}         

解决方法:

这篇博客:The EventSource NuGet package and support for the Windows Event Log (Channel Support)有一个链接到一个罕见的EventSource User’s Guide文件,其中说明了这一点:

Do use the EventSourceAttribute’s Name property to provide a
descriptive, qualified name for the ETW event provider represented by
your event source. The default is the short name of your event source
type, which can easily lead to collisions, as ETW provider names share
one machine-wide namespace. An example of a good provider name
<CompanyName>-<Product>-<Component>”. Following this 3-element
convention will ensure Event Viewer displays your event logs in a
logical folder hierarchy: “Application and Services
Logs/<CompanyName>/<Product>/<Component>
”.

这往往表明破折号更像是一种惯例而不是严格的要求(所以我相信你可以自己解析它).请注意,博客仍然可以发表评论.

对于不匹配的名称,有一个未记录的EvtIntGetClassicLogDisplayName函数可以获取事件查看器中显示的名称.以下是如何将它与会话和日志名称一起使用:

    static void Main(string[] args)
    {
        var session = new EventLogSession();
        foreach (string name in session.GetLogNames())
        {
            Console.WriteLine(GetDisplayName(session, name));
        }
    }

这里是支持代码(因为它没有文档,使用风险自负,加上它似乎主要用于这个’OAlert’条目,所以我不确定它是否值得):

    public static string GetDisplayName(EventLogSession session, string logName)
    {
        var sb = new StringBuilder(512);
        int bufferUsed = 0;
        if (EvtIntGetClassicLogDisplayName(GetSessionHandle(session).DangerousGetHandle(), logName, 0, 0, sb.Capacity, sb, out bufferUsed))
            return sb.ToString();

        return logName;
    }

    private static SafeHandle GetSessionHandle(EventLogSession session)
    {
        return (SafeHandle)session.GetType().GetProperty("Handle", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(session);
    }

    [DllImport("wevtapi.dll", CharSet = CharSet.Unicode)]
    private static extern bool EvtIntGetClassicLogDisplayName(IntPtr session, [MarshalAs(UnmanagedType.LPWStr)] string logName, int locale, int flags, int bufferSize, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder displayName, out int bufferUsed);

标签:c,net,system-diagnostics,event-viewer,etw
来源: https://codeday.me/bug/20190519/1135048.html