.Net Core 2.2.0

.Net Core 2.2.0已经发布有一段时间了,很多新鲜功能已经有博主介绍了,今天给大家介绍一下运行时事件并附上demo。

运行时事件

通常需要监视运行时服务(如当前进程的GC,JIT和ThreadPool),以了解这些服务在运行应用程序时的行为方式。在Windows系统上,这通常使用ETW并监视当前进程的ETW事件来完成。虽然这种方法仍然有效,但使用ETW并不总是容易或可能。无论您是在低权限环境中运行还是在Linux或macOS上运行,都可能无法使用ETW。

从.NET Core 2.2开始,现在可以使用EventListener类来使用CoreCLR事件。这些事件描述了GC,JIT,ThreadPool和interop的行为。它们是在Windows上作为CoreCLR ETW提供程序的一部分公开的相同事件。这允许应用程序使用这些事件或使用传输机制将它们发送到遥测聚合服务。

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.

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.

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");
        }
    }
}

2020.04.03补充:
https://www.cnblogs.com/artech/p/performance-counter-in-net-core.html

参考

https://www.cnblogs.com/justmine/p/10069160.html
https://www.cnblogs.com/viter/p/10140697.html
https://www.tenforums.com/windows-10-news/122856-announcing-net-core-2-2-a.html

posted on 2018-12-28 11:09  kingreatwill  阅读(1585)  评论(0编辑  收藏  举报