Add Field To All ETW Events In EventSource

125 Views Asked by At

Currently, my ETW events are defined like this:

public class MyEventSource: EventSource
{
    [Event(1, Level=Info)]
    public void WriteMyEvent(string Message)
    {
        WriteEvent(1, Message);
    }

    [Event(2, Level=Info)]
    public void WriteOtherEvent(string Details) 
    {
        WriteEvent(2, Details);
    }
}

I'd like to add a field to both of these events that callers do not need to pass in. It is a static string that is defined in app configuration. I've found that there is no way to add it to every WriteEvent call other than adding it to the parameters of the [Event] decorated function.

public class MyEventSource: EventSource
{
    public static readonly string MyDefault = "something";

    [Event(1, Level=Info)]
    public void WriteMyEvent(string Message, string Default = null)
    {
        // Ignore input "Default", use "MyDefault" instead
        WriteEvent(1, Message, MyDefault);
    }

    [Event(2, Level=Info)]
    public void WriteOtherEvent(string Details, string Default = null) 
    {
        // Ignore "Default"
        WriteEvent(2, Details, MyDefault);
    }
}

If I pipe these through a base [NonEvent] method, they garble the output. The above code block is the only way I've found of adding the field to every Event. There has to be a better way, but I have been unable as of yet to come up with the perfect Google search to find out how this is done.

0

There are 0 best solutions below