如何在.net core 2.2(运行时事件)中使用EventListener

How to use EventListener in .net core 2.2 (Runtime Events)

MS 宣布发布 .NET Core 2.2。它包括对运行时的诊断改进,Runtime Events,我的问题是:如何在 .NET Core 2.2 中使用 EventListener?文章很烂

我实际上在示例 ASP.Net Core 2.2 API 项目中使用它。这实际上相当容易,但我也需要一些时间。

我创建了一个诊断程序 class,就像文档中提到的那样。我将其命名为 class Diagnostics.cs。 class 包含 Microsoft 文档中的代码。仅此而已。 然后我将它作为单例服务添加到 ConfigureServices() 中。

services.AddSingleton<Diagnostics>();

最后我把它注入到 ValuesController 中。

private Diagnostics _diag;
        public ValuesController(Diagnostics diag)
        {
            _diag = diag;
        }

从 Postman 到达端点时,我收到了很多事件。

ThreadID = 13588 ID = 192 Name = MethodJitInliningFailed
        Name = "MethodBeingCompiledNamespace" Value = "Microsoft.IntelliTrace.TelemetryObserver.MvcActionEventArgumentSerializer"
        Name = "MethodBeingCompiledName" Value = "SerializeArguments"
        Name = "MethodBeingCompiledNameSignature" Value = "class System.String  (class System.Collections.Generic.IEnumerable`1<value class System.Collections.Generic.KeyValuePair`2<class System.String,class System.Object>>)"
        Name = "InlinerNamespace" Value = "Microsoft.IntelliTrace.TelemetryObserver.MvcActionEventArgumentSerializer"
        Name = "InlinerName" Value = "SerializeArguments"
        Name = "InlinerNameSignature" Value = "class System.String  (class System.Collections.Generic.IEnumerable`1<value class System.Collections.Generic.KeyValuePair`2<class System.String,class System.Object>>)"
        Name = "InlineeNamespace" Value = "System.Collections.Generic.IEnumerator`1[System.Collections.Generic.KeyValuePair`2[System.__Canon,System.__Canon]]"
        Name = "InlineeName" Value = "get_Current"
        Name = "InlineeNameSignature" Value = "instance !0  ()"
        Name = "FailAlways" Value = "False"
        Name = "FailReason" Value = "target not direct"
        Name = "ClrInstanceID" Value = "8"

这当然只是为了测试目的。我可能不会将其添加为单例,而是尝试将其改为通用/类型化,并将其添加为作用域或瞬态服务。