title: How to disable Task Parallel Library's ETW EventSource in a Universal App?
body: <p>Task Parallel Library uses <a href="http://blogs.msdn.com/b/vancem/archive/2012/07/09/logging-your-own-etw-events-in-c-system-diagnostics-tracing-eventsource.aspx" rel="nofollow noreferrer">Event Tracing for Windows (ETW)</a> for logging. Apparently, there is a bug related to logging, in either TPL or ETW, surfacing under Windows Phone or Windows Store .NET Runtime. The original issue is described <a href="https://stackoverflow.com/q/28269415/1768303">here</a>.</p>
  
  <p>A possible workaround would be to disable the TPL's ETW <code>EventSource</code>. </p>
  
  <p><strong>How do I disable it from within a Universal Windows app</strong>, if I really want to? </p>
  
  <p>Reflection works for a Desktop app, but not for a WP/WinRT app. <code>EventCommand.Disable</code> is not recognized as a valid command for <code>EventSource.SendCommand</code> (although it's used internally in ETW). Here's the code to play with (as a console app):</p>
  
  <pre><code>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&lt;string, string&gt;());
                          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();
          }
      }
  }
  </code></pre>
  
  <p>For a Universal app, <code>MyEventListener</code> can just be instantiated as part of <code>Application</code>:</p>
  
  <pre><code>public sealed partial class App : Application
  {
      static MyEventListener listener = new MyEventListener();
  }
  </code></pre>