Why is there a restriction on the type of parameter and the number of EventSource tags

I'm currently experimenting with Microsoft EventSources in C #. One limitation is the following

... The number and types of arguments passed to the ETW method must exactly match the types passed in the WriteEvent call that it calls. For instance:

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

This basically limits the ability to write a richer API in the EventSource class. This basically means that you cannot create a method that receives a custom object, and inside the body of the method you can serialize it to a string (or another type supported by WriteEvent overloads).

The only thing you can decide is the name of the method and the parameters and counts that reflect the WriteEvent overload. Or am I wrong?

+5
source share
2 answers

This is required to create a manifest file. _EventSourceUsersGuide.docx explains this:

Event methods must exactly match the types of WriteEvent overloaded records, in particular you should avoid implicit scalar transformations; 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.

+2
source

This was due to magicandre1981. However, you are not forbidden to write the rich API that you describe. The solution is to provide overloads marked with the NonEventAttribute attribute. For instance:

  [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); } 
+9
source

Source: https://habr.com/ru/post/1207391/


All Articles