Skip to content

Disposing EventListener doesn't send disable command #56378

@noahfalk

Description

@noahfalk

Normally when ETW or EventPipe sessions end a disable command is sent to any EventSources that were being monitored to indicate that monitoring has stopped. EventListener also stops listening when it is disposed, but it doesn't provide any notification that it is doing so.

Repro:

using System.Diagnostics;
using System.Diagnostics.Tracing;

class Program
{
    static void Main(string[] args)
    {
        
        MyEventSource source = new MyEventSource();
        using (MyEventListener listener = new MyEventListener())
        {
            listener.EnableEvents(source, EventLevel.Informational);
            Debug.Assert(source.Enabled);
            // uncommenting this line will make the assert below pass, but it shouldn't be necessary
            //listener.DisableEvents(source);
        }
        Debug.Assert(!source.Enabled); // once the listener is disposed we expect OnEventCommand to be called with a disable command
    }
}

[EventSource(Name ="MyEventSource")]
public class MyEventSource : EventSource
{
    public bool Enabled;

    protected override void OnEventCommand(EventCommandEventArgs command)
    {
        if(command.Command == EventCommand.Enable)
        {
            Enabled = true;
        }
        else
        {
            Enabled = false;
        }
    }
}

public class MyEventListener : EventListener
{
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions