ETW使用TraceEvent按事件ID过滤

时间:2016-08-19 14:18:28

标签: c# .net windows etw

我使用Microsoft.Diagnostics.Tracing.EventSource nuget版本1.1.28发出ETW事件,并使用Microsoft.Diagnostics.Tracing.TraceEvent nuget版本1.0.41动态跟踪事件。我尝试做的是过滤事件ID将跟踪的事件,我似乎无法使其工作,所以我创建了一个简单的测试项目来测试它在使用.NET Framework 4.6版的Windows 10 Enterprise计算机上

我的活动源代码:

[EventSource(Name = ProviderName)]
public sealed class EtwDemoEventSource : EventSource
{
    private static Lazy<EtwDemoEventSource> instance = new Lazy<EtwDemoEventSource>(() => new EtwDemoEventSource());
    public const string ProviderName = "EtwDemoEventSource";

    private EtwDemoEventSource() { }

    public static EtwDemoEventSource Log
    {
        get
        {
            return instance.Value;
        }
    }

    [Event(1, Message = "Foo event raised.")]
    public void FooEvent()
    {
        WriteEvent(1);
    }

    [Event(2, Message = "Bar event raised.")]
    public void BarEvent()
    {
        WriteEvent(2);
    }
}

我有一个控制台应用程序使用事件源发出这些事件:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Start emitting events.");
        EtwDemoEventSource.Log.FooEvent();
        EtwDemoEventSource.Log.BarEvent();
        Console.WriteLine("Finish emitting events.");
        Console.ReadLine();
    }
}

另一个跟踪事件的应用程序:

class Program
{
    static void Main(string[] args)
    {
        using (var session= new TraceEventSession("EtwTraceEventSession"))
        {
            Console.WriteLine("Start collecting events.");
            session.Source.Dynamic.All += te => Console.WriteLine(te.FormattedMessage);
            session.EnableProvider(EtwDemoEventSource.ProviderName, options: new TraceEventProviderOptions { EventIDsToEnable = new int[] { 1 } });
            Console.CancelKeyPress += (sender, cargs) => session.Stop();
            session.Source.Process();
            Console.WriteLine("Finish collecting events.");
        }
    }
}

如果你看一下跟踪应用程序中的EnableProvider方法,我发现TraceEventProviderOptions有一个EventIDsToEnable属性,它似乎适合我的用例,但运行带有该代码的跟踪应用程序(首先我运行跟踪应用程序,然后发出事件的应用程序)不会产生跟踪的事件。如果我删除了TraceEventProviderOptions,那么所有事件都会被正确跟踪,如果我使用EventIDsToDisable属性,它也会按预期工作。

我在假设EventIDsToEnable属性应该如何影响事件过滤时出错(我似乎无法找到很多文档)或者我应该以不同的方式使用它吗?

0 个答案:

没有答案
相关问题