Skip to content

EventListener is filtering at LogAlways verbosity has special cased behavior that should be appropriately documented #2445

Closed
dotnet/dotnet-api-docs
#7924

Description

@noahfalk

EDIT: Originally I believed the special case behavior of LogAlways filtering in EventListener was in error and filed this bug to fix it. However it actually aligns with ETW behavior and past precedent so I now consider the existing behavior to be by-design. Instead this issue is repurposed to cover updating the docs so that they accurately describe the behavior as it exists. Below is my original bug report that shows the difference in behavior, even though I no longer plan to change it.


In EventListener, calling EnableEvents(eventSource, level) is intended to filter events so that only events equally or more severe than level invoke the OnEventWritten callback. However that filtering doesn't appear to be happening.

Repro code:

using System;
using System.Diagnostics.Tracing;

namespace ConsoleApp21
{
    class Program
    {
        static void Main(string[] args)
        {
            MyEventListener l = new MyEventListener(EventLevel.LogAlways);
            MyEventListener l2 = new MyEventListener(EventLevel.Error);
            MyEventListener l3 = new MyEventListener(EventLevel.Warning);
            MyEventListener l4 = new MyEventListener(EventLevel.Informational);
            MyEventListener l5 = new MyEventListener(EventLevel.Verbose);
            MyEventSource s = new MyEventSource();
            s.SayHello();
        }
    }

    class MyEventListener : EventListener
    {
        EventLevel _level;
        public MyEventListener(EventLevel level)
        {
            _level = level;
        }

        protected override void OnEventSourceCreated(EventSource eventSource)
        {
            if(eventSource.Name == "MyEventSource")
            {
                EnableEvents(eventSource, _level);
            }
        }

        protected override void OnEventWritten(EventWrittenEventArgs eventData)
        {
            Console.WriteLine($"{eventData.EventName} Level={eventData.Level} EnableLevel={_level}");
        }
    }

    [EventSource(Name ="MyEventSource")]
    class MyEventSource : EventSource
    {
        [Event(1, Level = EventLevel.Informational)]
        public void SayHello()
        {
            WriteEvent(1);
        }
    }
}

Expected behavior: Console should show:

SayHello Level=Informational EnableLevel=Informational
SayHello Level=Informational EnableLevel=Verbose

Actual behavior: Console shows:

SayHello Level=Informational EnableLevel=LogAlways
SayHello Level=Informational EnableLevel=Informational
SayHello Level=Informational EnableLevel=Verbose

Activity

  1. changed the title [-]EventListener is not filtering events from EventSource[/-] [+]EventListener is not filtering events from EventSource when enabled at LogAlways verbosity[/+] on Apr 17, 2021
  2. added a commit that references this issue on Jul 13, 2021
  3. self-assigned this
    on Jul 14, 2021
  4. KalleOlaviNiemitalo commented on Jul 14, 2021

    @KalleOlaviNiemitalo

    The special handling of LogAlways seems consistent with Windows Event Log ChannelPublishingType Complex Type:

    The severity level of the events to write to the channel. The service writes events to the channel that have a level value that is less than or equal to the specified value. The default is 0, which means to log events with any level value.
    The session passes the level value to the provider when it enables the provider.

    That makes me wonder whether there is a risk that removing the special handling of LogAlways from .NET makes an event provider not comply with this Windows documentation.

    However, EnableCallback and EnableTrace do not document this interpretation of a zero level. I don't know whether the documentation is incomplete or Windows Event Log maps the zero level in the manifest to a different value before it calls EnableCallback.

  5. noahfalk commented on Jul 14, 2021

    @noahfalk
    MemberAuthor

    @KalleOlaviNiemitalo - thanks! I did a little testing with ETW and I do see results suggesting that LogAlways is a special cased value there. I am going to reach out to one of the devs on Windows ETW team and see if I can get confirmation that the special casing is by design. If so then it probably means we have a bunch of documentation that is wrong and we'll repurpose the bug to address that instead.

  6. noahfalk commented on Jul 14, 2021

    @noahfalk
    MemberAuthor

    I did confirm with the ETW team that setting a level of LogAlways on a provider is treated as a special case that does no level-based filtering. I am taking a look through the runtime's code to see how our logic compares in other situations to figure out what we do next.

  7. noahfalk commented on Jul 21, 2021

    @noahfalk
    MemberAuthor

    I concluded we shouldn't make behavioral changes here for 6.0, instead we should pursue doc changes. Updating the title accordingly. Thanks for the timely and helpful info @KalleOlaviNiemitalo : )

  8. changed the title [-]EventListener is not filtering events from EventSource when enabled at LogAlways verbosity[/-] [+]EventListener is filtering at LogAlways verbosity has special cased behavior that should be appropriately documented[/+] on Jul 21, 2021
  9. transferred this issue fromdotnet/runtimeon Jul 21, 2021
  10. added
    documentation
    on Aug 6, 2021
  11. added this to the 6.0.0 milestone on Aug 6, 2021
  12. modified the milestones: .NET 6.0, .NET 7.0 on Jan 7, 2022
  13. mikelle-rogers commented on Mar 4, 2022

    @mikelle-rogers
    Member

    EventListener (it is mentioned but not descried). Include [init behavior] (dotnet/runtime#38804 (comment)). As mentioned in #515

  14. added a commit that references this issue on Mar 31, 2022
  15. added a commit that references this issue on Apr 4, 2022
  16. noahfalk commented on Apr 7, 2022

    @noahfalk
    MemberAuthor
  17. ghost locked as resolved and limited conversation to collaborators on Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

documentation

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions