1

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.

Dagrooms
  • 1,507
  • 2
  • 16
  • 42
  • what do you want to achieve? Log a separate event to log the information – magicandre1981 Aug 27 '19 at 13:30
  • I want every event to log the name of the environment it is coming from – Dagrooms Aug 27 '19 at 15:10
  • use ActivityID to group them and raise 1 event with the name of the environment so you see which Events have same ActivityID and from which environment they came. – magicandre1981 Aug 27 '19 at 21:31
  • That's a hack, and a searing headache to deal with 100+ microservices trying to correlate which logs came from my "prod" environment. – Dagrooms Aug 27 '19 at 22:14
  • no, ActivityID was added just for your case – magicandre1981 Aug 28 '19 at 07:52
  • I shouldn't have to join query results just to see which environment the event came from. Furthermore, you're presenting a hack to solve my use-case when my root question still stands: how do you add a field to every event without adding it as a default parameter to the [Event] decorated method and ignoring the parameter? – Dagrooms Aug 28 '19 at 15:36
  • it is not possible, because the manifest is generated based on the signature of the ETW event method. read [here more about Activities](https://blogs.msdn.microsoft.com/vancem/2015/09/14/exploring-eventsource-activity-correlation-and-causation-features/) – magicandre1981 Aug 29 '19 at 14:51

0 Answers0