Why the restriction on parameter type and count on EventSource Methods
Asked Answered
P

2

6

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?

Pd answered 21/11, 2014 at 10:15 Comment(1)
It may be worth trying out a later version of EventSource, specifically the Rich Data support: blogs.msdn.microsoft.com/vancem/2015/09/20/…Drew
S
4

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.

Snag answered 21/11, 2014 at 17:7 Comment(0)
D
13

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); 
        }
Daumier answered 8/1, 2015 at 23:41 Comment(1)
Thanks that help me resolve a "bad" example in the "Developer's Guide to Microsoft Enterprise Library - 2nd Edition" @page 130. Or maybe I missed something in their explanation.Homoousian
S
4

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.

Snag answered 21/11, 2014 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.