title: Detecting whether or not (and why) Application Insights can send telemetry data to Azure
body: <p>I'm developing a Windows Desktop application and have successfully linked the Application Insights Core assemblies. I'm using <code>TrackTrace</code>, <code>TrackEvent</code>, etc. to send custom telemetry.</p>
  
  <p>On some workstations telemetry is successfully sent to the Azure portal, and on some others it is not, although calls to <code>TrackTrace</code>, <code>Flush</code>, etc. succeed (or at least return without throwing an exception. <code>TelemetryClient.IsEnabled()</code> returns true. Both workstations use an <code>InMemoryChannel</code>, with the same endpoint                 <code>https://dc.services.visualstudio.com/v2/track</code> and the sending interval is 30 seconds.</p>
  
  <p>Is there an API function I can call in my application to get the connection status of the TelemetryClient? Something that would tell me that the client is successfully connected, or that it encountered error <strong>x</strong> while trying, and still has <strong>y</strong> telemetry packets waiting to be sent.</p>
  
  <p>I'm not looking for a checklist like <em>reinstall the NuGet package</em> (I did...), <em>Make sure your firewall allows traffic to port xxx</em> (it does...) or <em>try to install kb...871</em> (I did that too...). What I'd like is a status report that I can log somewhere on the client workstation while my application runs, at least acknowledging in the status bar (Yes I know status bars are so old fashioned these days) that there is a problem.</p>
  
  <p><strong>First update - Getting the queue size</strong></p>
  
  <p>First victory, I was able to get the queue size. I wanted to do that without creating my own channel implementation (yet). This however is of little help to detect outages, because the queue will drain even if the transmitter is unable to transmit the telemetry items (it will just discard them) - more on this later. At least you know the transmitter thread is running...</p>
  
  <pre><code>private ITelemetryChannel _TelemetryChannel;
  private InMemoryChannel _InMemoryChannel;
  private object _TelemetryBuffer;
  private object _BufferLock;
  private object _InMemoryTransmitter;
  
  _TelemetryChannel = TelemetryConfiguration.Active?.TelemetryChannel;
  if (_TelemetryChannel != null &amp;&amp; _TelemetryChannel is InMemoryChannel)
  {
    _InMemoryChannel = (InMemoryChannel)_TelemetryChannel;
    _TelemetryBuffer = GetInstanceField (_InMemoryChannel, "buffer");
    _BufferLock = GetInstanceField (_TelemetryBuffer, "lockObj");
    _InMemoryTransmitter = GetInstanceField (_InMemoryChannel, "transmitter");
  }
  
  public int GetTelemetryQueueSize ()
  {
      if (_BufferLock != null)
      {
          lock (_BufferLock)
          {
              object l = GetInstanceField (_TelemetryBuffer, "items");
              if (l is List&lt;ITelemetry&gt;)
              {
                  return ((List&lt;ITelemetry&gt;)l).Count;
              }
          }
      }
      return -1;
  }
  </code></pre>
  
  <p>You also need an utility function to use reflection to access the private fields of the objects (the buffer and transmitter are <code>internal sealed</code>...) I made these as error resistant as possible, they could be more concise.</p>
  
  <pre><code>private static object GetInstanceField (Type type, object instance, string fieldName)
  {
      if (instance == null)
      {
          return null;
      }
      try
      {
          BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
          FieldInfo field = type.GetField (fieldName, bindFlags);
          return field.GetValue (instance);
      }
      catch
      {
          return null;
      }
  }
  
  private static object GetInstanceField (object instance, string fieldName)
  {
      if (instance == null)
      {
          return null;
      }
      return GetInstanceField (instance.GetType (), instance, fieldName);
  }
  </code></pre>