Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the restriction on parameter type and count on EventSource Methods

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?

like image 216
rudimenter Avatar asked Nov 21 '14 10:11

rudimenter


2 Answers

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); 
        }
like image 195
Frank Hileman Avatar answered Oct 02 '22 14:10

Frank Hileman


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.

like image 37
magicandre1981 Avatar answered Oct 02 '22 13:10

magicandre1981