Most of the time, you may want to monitor your application's use of runtime services, such as the GC, JIT, and ThreadPool, to understand how they impact your application.
In .NET Core 2.2, CoreCLR events can now be consumed using the System.Diagnostics.Tracing.EventListener
class.
The following example shows how to subscribe to events in the following code sample.
internal sealed class SimpleEventListener : EventListener
{
// Called whenever an EventSource is created.
protected override void OnEventSourceCreated(EventSource eventSource)
{
// Watch for the .NET runtime EventSource and enable all of its events.
if (eventSource.Name.Equals("Microsoft-Windows-DotNETRuntime"))
{
EnableEvents(eventSource, EventLevel.Verbose, (EventKeywords)(-1));
}
}
// Called whenever an event is written.
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
// Write the contents of the event to the console.
Console.WriteLine($"ThreadID = {eventData.OSThreadId} ID = {eventData.EventId} Name = {eventData.EventName}");
for (int i = 0; i < eventData.Payload.Count; i++)
{
string payloadString = eventData.Payload[i]?.ToString() ?? string.Empty;
Console.WriteLine($"\tName = \"{eventData.PayloadNames[i]}\" Value = \"{payloadString}\"");
}
Console.WriteLine("\n");
}
}
In .NET Core 2.2 the following two properties are also added to the EventWrittenEventArgs
class to provide additional information about ETW events:
EventWrittenEventArgs.OSThreadId
EventWrittenEventArgs.TimeStamp