.net core 运行时事件(Runtime Events)

.Net Core 2.2.0

.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

Runtime Events

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

Demo:

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

相关文章
相关标签/搜索