System.Diagnostics.Tracing.EventSource.IsEnabled 是如何工作的?
How does System.Diagnostics.Tracing.EventSource.IsEnabled work?
使用自定义事件源时,例如:
[EventSource(Name = "MyEventSource")]
public partial class CustomEventSource : EventSource
{
}
EventSource 上有一个 IsEnabled 方法class:
EventSource.IsEnabled(eventLevel, eventKeywords)
https://msdn.microsoft.com/en-us/library/hh393402(v=vs.110).aspx
该方法如何判断事件是否为'Enabled'级别和关键字?似乎没有关于此的任何可靠文档。在我的实现中,该方法是 returning false,我不确定需要做什么才能使其 return true。
来自 public 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.
请注意,false return 是准确的,因此您需要查看您的级别和关键字。
@Hans 是正确的。我忘记指出您需要开始收集事件才能启用它们。您可以通过编程方式执行此操作,也可以使用一系列工具(例如 PerfView)。
您似乎需要将 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
}
编辑:
我还找到了 EvenSource.SendCommand
方法,它可以将 EventCommand.Enable
作为参数,但它只会为我抛出一个 ArgumentException
。是的,EventSource
的文档真的 很糟糕。
使用自定义事件源时,例如:
[EventSource(Name = "MyEventSource")]
public partial class CustomEventSource : EventSource
{
}
EventSource 上有一个 IsEnabled 方法class:
EventSource.IsEnabled(eventLevel, eventKeywords)
https://msdn.microsoft.com/en-us/library/hh393402(v=vs.110).aspx
该方法如何判断事件是否为'Enabled'级别和关键字?似乎没有关于此的任何可靠文档。在我的实现中,该方法是 returning false,我不确定需要做什么才能使其 return true。
来自 public 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.
请注意,false return 是准确的,因此您需要查看您的级别和关键字。
@Hans 是正确的。我忘记指出您需要开始收集事件才能启用它们。您可以通过编程方式执行此操作,也可以使用一系列工具(例如 PerfView)。
您似乎需要将 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
}
编辑:
我还找到了 EvenSource.SendCommand
方法,它可以将 EventCommand.Enable
作为参数,但它只会为我抛出一个 ArgumentException
。是的,EventSource
的文档真的 很糟糕。