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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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



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.





It takes two to do this tango, the event source and the event listener. The source doesn't do the work until a somebody signals interest in the events. If the listener is a .net program then you'd use EventListener.EnableEvents(). Or you'd for example use the Windows logman.exe utility to create a data collector, try that first.
– Hans Passant
7 mins ago




2 Answers
2



From the source code for public bool IsEnabled(EventLevel level, EventKeywords keywords):


bool IsEnabled(EventLevel level, EventKeywords keywords)



Returns true if events with greater than or equal 'level' and have one
of 'keywords' set are enabled.



Note that the result of this function is only an approximation on
whether a particular event is active or not. It is only meant to be
used as way of avoiding expensive computation for logging when logging
is not on, therefore it sometimes returns false positives (but is
always accurate when returning false). EventSources are free to have
additional filtering.



Note that false return is accurate so you need to look at your level and keywords.



@Hans is correct. I have neglected to point out that you need to start collecting the events for them to be enabled. You can do this programmatically or make use of a range of tools such as PerfView.



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


EventListener


EventSource


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.


EvenSource.SendCommand


EventCommand.Enable


ArgumentException


EventSource






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

'Series' object is not callable Error / Statsmodels illegal variable name