如何在通用应用程序中禁用任务并行库的ETW EventSource?

5

任务并行库使用Windows事件跟踪(ETW)进行日志记录。显然,在Windows Phone或Windows Store .NET Runtime下,TPL或ETW存在与日志记录相关的bug。原始问题在此处描述。

一个可能的解决方法是禁用TPL的ETW EventSource

如果我真的想这样做,如何在通用Windows应用程序中禁用它?

反射适用于桌面应用程序,但不适用于WP / WinRT应用程序。EventCommand.Disable不被认为是EventSource.SendCommand的有效命令(尽管在ETW中内部使用)。以下是要使用的代码(作为控制台应用程序):

using System;
using System.Threading.Tasks;
using System.Diagnostics.Tracing;
using System.Reflection;

namespace ConsoleApplication
{
    class Program
    {
        internal class MyEventListener : EventListener
        {
            protected override void OnEventSourceCreated(EventSource eventSource)
            {
                Console.WriteLine(eventSource);
                base.OnEventSourceCreated(eventSource);
                if (eventSource.Name == "System.Threading.Tasks.TplEventSource")
                {
                    Console.WriteLine("enabled: " + eventSource.IsEnabled());

                    // trying to disable with EventCommand.Disable: Invalid command
                    try
                    {
                        System.Diagnostics.Tracing.EventSource.SendCommand(
                            eventSource, EventCommand.Disable,
                            new System.Collections.Generic.Dictionary<string, string>());
                        Console.WriteLine("enabled: " + eventSource.IsEnabled());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    // reflection: doesn't work for Windows Phone/Store apps
                    try
                    {
                        var ti = typeof(EventSource).GetTypeInfo();
                        var f = ti.GetDeclaredField("m_eventSourceEnabled");
                        f.SetValue(eventSource, false);
                        Console.WriteLine("enabled: " + eventSource.IsEnabled());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }

            protected override void OnEventWritten(EventWrittenEventArgs eventData)
            {
                Console.WriteLine(eventData);
            }
        }

        static MyEventListener listener = new MyEventListener();

        static void Main(string[] args)
        {
            Task.Delay(1000).Wait();
            Console.ReadLine();
        }
    }
}

对于一款通用应用程序,MyEventListener 可以作为 Application 的一部分被实例化:
public sealed partial class App : Application
{
    static MyEventListener listener = new MyEventListener();
}
1个回答

2
我遇到了类似的问题并找到了一个潜在的解决方案。
你可以简单地调用Dispose()来关闭EventSource!
这不会删除事件源,但它会禁用它。基本的EventSource类已经有适当的检查措施来防止在其禁用时调用其余继承类。所以理论上来说是安全的。但是可能有一些EventSource实现无法正常工作,因此请充分测试!
请参见这里EventSource.Dispose(bool disposing)实现。

我没有机会为UWP检查这个,但似乎在桌面上运行良好。他们最近是否为EventListener添加了IDispose支持?我不记得3年前看到过 :) - noseratio - open to work

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接