ETW日志记录 - TraceEventSession为侦听器启用多个事件提供程序

时间:2015-01-07 08:11:04

标签: c# logging etw etw-eventsource

我正在开发一个ETW监听器来监听系统中所有可用的事件源。

Q1: 请在下面找到示例代码:

providerName = "ETW-TEST-APPLICATION";
sessionName = "ETW-TEST-APPLICATION";

using (var session = new TraceEventSession(sessionName, null))
{
    try
    {
        session.StopOnDispose = true;
        session.BufferSizeMB = 100;
        Console.CancelKeyPress +=
            delegate(object sender, ConsoleCancelEventArgs e) { session.Dispose(); };

        using (source = new ETWTraceEventSource(sessionName, TraceEventSourceType.Session))
        {

            var parser = new DynamicTraceEventParser(source);
            parser.AddDynamicProvider(new ProviderManifest(Environment.CurrentDirectory +
                                                           "\\ETWApplicationRegister." +
                                                           providerName +
                                                           ".etwManifest.man"));

            parser.All += delegate(TraceEvent data)
            {
                Console.WriteLine("--------------------------------------------------------------");
                Console.WriteLine("ProviderName : " + data.ProviderName);
                Console.WriteLine("ProviderGuid : " + data.ProviderGuid);
                Console.WriteLine("EventID : " + data.ID);
                Console.WriteLine("Level : " + data.Level);
                Console.WriteLine("Channel : " + data.Channel);
                Console.WriteLine("Task : " + data.Task);
                Console.WriteLine("TaskName : " + data.TaskName);
                Console.WriteLine("TimeStamp : " + data.TimeStamp);
                Console.WriteLine("FormattedMessage : " + data.FormattedMessage);
                Console.WriteLine("PayLoad : " + data.PayloadByName("jsonArgs"));
                Console.WriteLine("--------------------------------------------------------------");
            };

            session.EnableProvider(providerGuid);

            source.Process(); //On execution of this statement system listens to the events
        }
    }
}

此代码适用于一个事件源,即" ETW-TEST-APPLICATION"。我希望这个监听器可用于多个事件源" ETW-TEST-APPLICATION2",#34; ETW-TEST-APPLICATION3"等

请告诉我有关实现此目的的选项。我尝试使用线程但没有工作。

Q2: 也让我知道如果我可以有多个session.EnableProvider,我想只启用信息和&错误消息,但忽略警告消息session.EnableProvider(providerGuid,TraceEventLevel.Informational); session.EnableProvider(providerGuid,TraceEventLevel.Error);

1 个答案:

答案 0 :(得分:2)

Q1:如果多个提供商在一个会话中,您不应该需要多个ETWTraceEventSource来启用它们。也就是说,如果你被迫,你可能需要为每个源创建一个新的DynamicTraceEventParser,然后将你的事件处理程序挂钩到每个解析器。

Q2:以下是我通过搜索程序集启用多个提供程序的简化示例:

    void EnableAllEventSources()
    {
        foreach (var es in GetEventSourceNamesFromAssembly(GetType().Assembly);)
            _session.EnableProvider(es);
    }

    IEnumerable<string> GetEventSourceNamesFromAssembly(Assembly assembly)
    {
        return assembly.GetTypes()
            .Where(t => t.BaseType == (typeof(EventSource)))
            .Select(t => {
                var attribute = Attribute.GetCustomAttribute(t, typeof(EventSourceAttribute));
                return ((EventSourceAttribute)attribute).Name;
            });
    }