internal void ForceRemoveBuggyClient(IActivityMonitorClient client)
        {
            Debug.Assert(client != null && _clients.Contains(client));
            IActivityMonitorBoundClient bound = client as IActivityMonitorBoundClient;

            if (bound != null)
            {
                try
                {
                    bound.SetMonitor(null, true);
                }
                catch (Exception ex)
                {
                    ActivityMonitor.CriticalErrorCollector.Add(ex, "While removing the buggy client.");
                }
            }
            if (_clients.Length == 1)
            {
                _clients = Util.Array.Empty <IActivityMonitorClient>();
            }
            else
            {
                int idx      = Array.IndexOf(_clients, client);
                var newArray = new IActivityMonitorClient[_clients.Length - 1];
                Array.Copy(_clients, 0, newArray, 0, idx);
                Array.Copy(_clients, idx + 1, newArray, idx, newArray.Length - idx);
                _clients = newArray;
            }
        }
 /// <summary>
 /// Unregisters the given <see cref="IActivityMonitorClient"/> from the <see cref="Clients"/> list.
 /// Silently ignores unregistered client.
 /// </summary>
 /// <param name="client">An <see cref="IActivityMonitorClient"/> implementation.</param>
 /// <returns>The unregistered client or null if it has not been found.</returns>
 public IActivityMonitorClient UnregisterClient(IActivityMonitorClient client)
 {
     if (client == null)
     {
         throw new ArgumentNullException("client");
     }
     using (_monitor.ReentrancyAndConcurrencyLock())
     {
         int idx;
         if ((idx = Array.IndexOf(_clients, client)) >= 0)
         {
             LogFilter filter = LogFilter.Undefined;
             IActivityMonitorBoundClient bound = client as IActivityMonitorBoundClient;
             if (bound != null)
             {
                 filter = bound.MinimalFilter;
                 bound.SetMonitor(null, false);
             }
             var newArray = new IActivityMonitorClient[_clients.Length - 1];
             Array.Copy(_clients, 0, newArray, 0, idx);
             Array.Copy(_clients, idx + 1, newArray, idx, newArray.Length - idx);
             _clients = newArray;
             if (filter != LogFilter.Undefined)
             {
                 _monitor.OnClientMinimalFilterChanged(filter, LogFilter.Undefined);
             }
             return(client);
         }
         return(null);
     }
 }
 private IActivityMonitorClient DoRegisterClient(IActivityMonitorClient client, ref bool forceAdded)
 {
     if ((forceAdded |= (Array.IndexOf(_clients, client) < 0)))
     {
         IActivityMonitorBoundClient bound = client as IActivityMonitorBoundClient;
         if (bound != null)
         {
             // Calling SetMonitor before adding it to the client first
             // enables the monitor to initialize itself before being solicited.
             // And if SetMonitor method calls InitializeTopicAndAutoTags, it does not
             // receive a "stupid" OnTopic/AutoTagsChanged.
             bound.SetMonitor(_monitor, false);
         }
         var newArray = new IActivityMonitorClient[_clients.Length + 1];
         Array.Copy(_clients, 0, newArray, 1, _clients.Length);
         newArray[0] = client;
         _clients    = newArray;
         if (bound != null)
         {
             _monitor.OnClientMinimalFilterChanged(LogFilter.Undefined, bound.MinimalFilter);
         }
     }
     return(client);
 }