0

I have this class for triggering ETW events:

public sealed class EventSourceWriter : EventSource
{
    public static EventSourceWriter Logger = new EventSourceWriter();

    [Event(1, Version = 0, Level = EventLevel.Informational)]
    public void Log(string Log)
    {
        WriteEvent(1, Log);
    }
}

As you can see, I set the EventLevel on top of the Log method as attribute value. Is there a way I can set it dynamically to log different EventLevels to same Event?

The idea is to see all generic logs on same table as output when an agent captures the ETW events.

Amit
  • 25,106
  • 25
  • 75
  • 116
  • 1
    In .NET 4.6 they added support for [dynamic events](http://blogs.msdn.com/b/vancem/archive/2015/10/02/dynamically-defined-events-in-eventsource-v4-6.aspx). Note that there is a guidance document linked there which says "we still recommend using the ‘contract based’ approach whenever that is possible." – Mike Zboray Feb 04 '16 at 21:19

1 Answers1

1

As mike-z has pointed out for .NET 4.6 you might archive that by using dynamic events, but in this case those events won't be included into the manifest, and not all of ETW tools have been updated to support self-describing events yet.

But generally you are writing your EventSource class like this to archive that:

public sealed class EventSourceWriter : EventSource
{
    public static EventSourceWriter Logger = new EventSourceWriter();

    [Event(1, Level = EventLevel.Informational)]
    public void LogInformational(string message)
    {
        WriteEvent(1, message);
    }

    [Event(2, Level = EventLevel.Warning)]
    public void LogWarning(string message)
    {
        WriteEvent(2, message);
    }

    [Event(3, Level = EventLevel.Error)]
    public void LogError(string message)
    {
        WriteEvent(3, message);
    }
}
Community
  • 1
  • 1