.Net Core 2.2.0已經發布有一段時間了,不少新鮮功能已經有博主介紹了,今天給你們介紹一下運行時事件並附上demo。html
一般須要監視運行時服務(如當前進程的GC,JIT和ThreadPool),以瞭解這些服務在運行應用程序時的行爲方式。在Windows系統上,這一般使用ETW並監視當前進程的ETW事件來完成。雖然這種方法仍然有效,但使用ETW並不老是容易或可能。不管您是在低權限環境中運行仍是在Linux或macOS上運行,均可能沒法使用ETW。windows
從.NET Core 2.2開始,如今能夠使用EventListener類來使用CoreCLR事件。這些事件描述了GC,JIT,ThreadPool和interop的行爲。它們是在Windows上做爲CoreCLR ETW提供程序的一部分公開的相同事件。這容許應用程序使用這些事件或使用傳輸機制將它們發送到遙測聚合服務。app
It is often desirable to monitor runtime services such as the GC, JIT, and ThreadPool of the current process to understand how these services are behaving while running your application. On Windows systems, this is commonly done using ETW and monitoring the ETW events of the current process. While this continues to work well, it is not always easy or possible to use ETW. Whether you’re running in a low-privilege environment or running on Linux or macOS, it may not be possible to use ETW.ide
Starting with .NET Core 2.2, CoreCLR events can now be consumed using the EventListener class. These events describe the behavior of GC, JIT, ThreadPool, and interop. They are the same events that are exposed as part of the CoreCLR ETW provider on Windows. This allows for applications to consume these events or use a transport mechanism to send them to a telemetry aggregation service.this
using System; using System.Diagnostics.Tracing; namespace ConsoleApp { internal class Program { private static void Main(string[] args) { SimpleEventListener l = new SimpleEventListener(); add(); Console.ReadLine(); } public static string add() { return "123"; } } 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"); } } }
http://www.javashuo.com/article/p-suvognza-ey.html
http://www.javashuo.com/article/p-urqarifh-dt.html
https://www.tenforums.com/windows-10-news/122856-announcing-net-core-2-2-a.htmlspa