How does System.Diagnostics.Tracing.EventSource.IsEnabled work?

Glen Thomas

When using a custom event source e.g.:

[EventSource(Name = "MyEventSource")]
public partial class CustomEventSource : EventSource
{
}

There is an IsEnabled method on the EventSource class:

EventSource.IsEnabled(eventLevel, eventKeywords)

https://msdn.microsoft.com/en-us/library/hh393402(v=vs.110).aspx

How does this method determine whether the event is 'Enabled' for the level and keywords? There doesn't seem to be any solid documentation on this. On my implementation the method is returning false and I am not sure what needs to be done in order to make it return true.

Freggar

Seems like you need to attach an EventListener to your EventSource to enable it:

class CustomEventListener : EventListener
{
    protected override void OnEventWritten(EventWrittenEventArgs eventData)
    {
    }
}

void Main() 
{
    var src = new CustomEventSource();
    var listener = new CustomEventListener();
    Console.WriteLine(src.IsEnabled(EventLevel.LogAlways, EventKeywords.None)); // false
    listener.EnableEvents(src, EventLevel.Error);

    Console.WriteLine(src.IsEnabled(EventLevel.LogAlways, EventKeywords.None)); // true
    Console.WriteLine(src.IsEnabled(EventLevel.Critical, EventKeywords.None)); // true
    Console.WriteLine(src.IsEnabled(EventLevel.Verbose, EventKeywords.None)); // false
}

EDIT:

I also found the EvenSource.SendCommand method which can take EventCommand.Enable as an argument but that only throws an ArgumentException for me. Yes, the documentation for EventSource is really bad.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

System.Diagnostics.Tracing.EventSource vs System.Diagnostics.Trace

Mono could not load type 'System.Diagnostics.Tracing.EventSource'

Can't register listeners to System.Diagnostics.Tracing.EventSource

System.Diagnostics.Tracing in Mono

Could not load file or assembly 'Microsoft.Diagnostics.Tracing.EventSource

Microsoft.Diagnostics.Tracing.EventSource with the RabbitMQ.Client.dll exception

Razor not compiling System.Diagnostics.Tracing.EventLevel

How does ltrace (library tracing tool) work?

Binding IsEnabled on ComboBoxItem does not work

how to fix this error "Microsoft.Extensions.Azure: Could not load file or assembly 'System.Diagnostics.Tracing, Version=5.0.0.0,"

EventSource tracing with correlated activity id

UIButton's isEnabled feature does not work

How does Dell's Pre Boot System Diagnostics "resolve" memory errors?

How to create a System.Diagnostics.Process array

Microsoft.Diagnostics.EventFlow with Inputs.EventSource

Why my System.Diagnostics.Process.Start cannot work?

How does the file system connector sink work

How does tell() work in Akka actor system?

How does the Lumen/Laravel Logging system work?

how does execlp() system call work?

How does System.out.print() work?

How does sleep system call work?

Humble bundles donations system, how does it work?

How does IPC::System::Simple capturex work?

How does %m work in $display system task?

How does the coordinate system work in OpenGL?

UIKit coordinate system, how does it work?

Tracing for an ASP.NET application does not work in production server

Where does System.Diagnostics.Debug.Write output appear?