Skip to content

[EventListener] We should add a new method ListenEvents() #38804

@John0King

Description

@John0King

Description

when I looking this doc https://github.com/dotnet/diagnostics/blob/master/documentation/design-docs/eventcounters.md#sample-code, I find that there is a private field in the construct that this sample code do not use,
after I "correct" the code , the SimpleEventListener won't work. After debug , I find that the method OnEventSourceCreated called before the constructor code, and this is caused by the the base constructor eg. EventCounter()/base() were doing all the work to call OnEventSourceCreated , and the only way to fix is to use event EventSourceCreated.

this behavior is against the normal code execution, that constructor should run first , and cause error behaviors for private readonly field

 public class SimpleEventListener : EventListener
{
        private readonly int _intervalSec;

        public SimpleEventListener()
        {
            _intervalSec = 1;  // this field should be 1 only , but   it also can be 0
        }

         protected override void OnEventSourceCreated(EventSource source)
        {
            var a = _internalSec; //   the a  is  0  when this method call
        }
}

so I think we should add a new method ListenEvents() , to start listen after the instance being created

using(var listener = new SimpleListenner())
{
    listener.ListenEvents()
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions