/// <summary>
        /// Initializes a new instance of the <see cref="PersistenceTransmitter"/> class.
        /// </summary>
        /// <param name="storage">The transmissions storage.</param>
        /// <param name="sendersCount">The number of senders to create.</param>
        /// <param name="createSenders">A boolean value that indicates if this class should try and create senders. This is a workaround for unit tests purposes only.</param>
        internal PersistenceTransmitter(StorageBase storage, int sendersCount, bool createSenders = true)
        {
            this.storage = storage;
            this.sendingCancellationTokenSource = new CancellationTokenSource();
            this.eventToKeepMutexThreadAlive = new AutoResetEvent(false);
            try
            {
                this.mutex = new Mutex(initiallyOwned: false, name: this.storage.FolderName);
            }
            catch (Exception e)
            {
                string errorMsg = string.Format(CultureInfo.InvariantCulture, "PersistenceTransmitter: Failed to construct the mutex: {0}", e);
                CoreEventSource.Log.LogVerbose(errorMsg);
            }

            if (createSenders)
            {
                Task.Factory.StartNew(() => this.AcquireMutex(() => this.CreateSenders(sendersCount)), TaskCreationOptions.LongRunning)
                .ContinueWith(
                    task =>
                    {
                        string msg = string.Format(CultureInfo.InvariantCulture, "PersistenceTransmitter: Unhandled exception in CreateSenders: {0}", task.Exception);
                        CoreEventSource.Log.LogVerbose(msg);
                    },
                    TaskContinuationOptions.OnlyOnFaulted);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FlushManager"/> class.
        /// </summary>        
        /// <param name="storage">The storage that persists the telemetries.</param>
        /// <param name="telemetryBuffer">In memory buffer that holds telemetries.</param>
        /// <param name="supportAutoFlush">A boolean value that determines if flush will happen automatically. Used by unit tests.</param>
        internal FlushManager(StorageBase storage, TelemetryBuffer telemetryBuffer, bool supportAutoFlush = true)
        {
            this.storage = storage;
            this.telemetryBuffer = telemetryBuffer;
            this.telemetryBuffer.OnFull = this.OnTelemetryBufferFull;
            this.FlushDelay = TimeSpan.FromSeconds(30);

            if (supportAutoFlush)
            {
                Task.Factory.StartNew(this.FlushLoop, TaskCreationOptions.LongRunning)
                    .ContinueWith(t => CoreEventSource.Log.LogVerbose("FlushManager: Failure in FlushLoop: Exception: " + t.Exception.ToString()), TaskContinuationOptions.OnlyOnFaulted);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Sender"/> class.
        /// </summary>
        /// <param name="storage">The storage that holds the transmissions to send.</param>
        /// <param name="transmitter">
        /// The persistence transmitter that manages this Sender. 
        /// The transmitter will be used as a configuration class, it exposes properties like SendingInterval that will be read by Sender.
        /// </param>
        /// <param name="startSending">A boolean value that determines if Sender should start sending immediately. This is only used for unit tests.</param>
        internal Sender(StorageBase storage, PersistenceTransmitter transmitter, bool startSending = true)
        {
            this.stopped = false;
            this.DelayHandler = new AutoResetEvent(false);
            this.stoppedHandler = new AutoResetEvent(false);
            this.drainingTimeout = TimeSpan.FromSeconds(100);
            this.defaultSendingInterval = TimeSpan.FromSeconds(5);

            // TODO: instead of a circualr reference, pass the TelemetryConfiguration.
            this.transmitter = transmitter;
            this.storage = storage;

            if (startSending)
            {
                Task.Factory.StartNew(this.SendLoop, TaskCreationOptions.LongRunning)
                    .ContinueWith(t => CoreEventSource.Log.LogVerbose("Sender: Failure in SendLoop: Exception: " + t.Exception.ToString()), TaskContinuationOptions.OnlyOnFaulted);
            }
        }