Enable EventListener filtering by sywhang · Pull Request #39117 · dotnet/runtime (original) (raw)

This logic still has some trouble. One example would be m_level =Informational, m_keywords=0x0, level = verbose, keywords=0x0. Expected result: false (listener level is too low), Actual result: true.

You probably should be calling EventSource.IsEnabledCommon() except reviewing that code it also appears to have bugs here and here : (. EventPipe native code has a correct implementation though the special case in the levelEnabled check for LogAlways is unnecessary complexity. LogAlways is the lowest value so sessionLevel >= LogAlways is always true.

The fact that we've got all these all these filtering bugs lurking probably means we are in need of EventListener level/keyword filtering tests for both the self-describing case and the manifested events.

I think you could fix IsEnabledCommon() to look like this:

        private static bool IsEnabledCommon(bool enabled, EventLevel currentLevel, EventKeywords currentMatchAnyKeyword,
                                                          EventLevel eventLevel, EventKeywords eventKeywords, EventChannel eventChannel)
        {
            if (!enabled)
                return false;

            // does is pass the level test?
            if (currentLevel < eventLevel)
                return false;

            // if yes, does it pass the keywords test?
            if (eventKeywords != 0)
            {
#if FEATURE_MANAGED_ETW_CHANNELS
                // is there a channel with keywords that match currentMatchAnyKeyword?
                if (eventChannel != EventChannel.None && this.m_channelData != null && this.m_channelData.Length > (int)eventChannel)
                {
                    EventKeywords channel_keywords = unchecked((EventKeywords)(m_channelData[(int)eventChannel] | (ulong)eventKeywords));
                    if (channel_keywords != 0 && (channel_keywords & currentMatchAnyKeyword) == 0)
                        return false;
                }
                else
#endif
                {
                    if ((unchecked((ulong)eventKeywords & (ulong)currentMatchAnyKeyword)) == 0)
                        return false;
                }
            }
            return true;
        }

Once you had that you could invoke it with Channel.None to get the right filtering behavior.