Why the restriction on parameter type and count on EventSource Methods

3.6k views Asked by At

I am experimenting at the moment with Microsoft EventSources in C#. One restriction is the following

...The number and types of arguments passed to the ETW method must exactly match the types passed to the WriteEvent overload it calls. For example:

[Event(2, Level = EventLevel.Informational)] 
public void Info(string message, int count) 
{
   base.WriteEvent(2, message, count); 
}

This basically limits you to writing a more rich API in the EventSource class. This basically means you can not create a method which receives a Custom Object and within the method body you can serialize it to a string (or another type supported by WriteEvent overloads).

The only thing you can decide is the method name and the parameternames and count which mirror an WriteEvent overloads. Or am I wrong?

2

There are 2 answers

0
magicandre1981 On BEST ANSWER

This is required to build the manifest file. The_EventSourceUsersGuide.docx explains it:

Event methods must match exactly the types of the WriteEvent overload it calls, in particular you should avoid implicit scalar conversions; they are dangerous because the manifest is generated based on the signature of the ETW event method, but the values passed to ETW are based on the signature of the WriteEvent overload.

1
Frank Hileman On

This was explained by magicandre1981. However, you are not prevented from writing the rich API you describe. The solution is to provide overloads marked with the NonEventAttribute. For example:

        [NonEvent]
        public void Warning(int tracerId, string message, params object[] args)
        {
            if (args != null)
                message = string.Format(message, args);
            Warning(tracerId, message);
        }

        [Event(EventIds.Warning, Level = EventLevel.Warning)]
        public void Warning(int tracerId, string message)
        {
            WriteEvent(EventIds.Warning, tracerId, message); 
        }