예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WindowsAzureTableSink"/> class with the specified connection string and table address.
        /// </summary>
        /// <param name="instanceName">The name of the instance originating the entries.</param>
        /// <param name="connectionString">The connection string for the storage account.</param>
        /// <param name="tableAddress">Either the name of the table, or the absolute URI to the table.</param>
        /// <param name="bufferInterval">The buffering interval to wait for events to accumulate before sending them to Azure Storage.</param>
        /// <param name="maxBufferSize">The maximum number of entries that can be buffered while it's sending to Azure Storage before the sink starts dropping entries.</param>
        /// <param name="onCompletedTimeout">Defines a timeout interval for when flushing the entries after an <see cref="OnCompleted"/> call is received and before disposing the sink.
        /// This means that if the timeout period elapses, some event entries will be dropped and not sent to the store. Normally, calling <see cref="IDisposable.Dispose"/> on
        /// the <see cref="System.Diagnostics.Tracing.EventListener"/> will block until all the entries are flushed or the interval elapses.
        /// If <see langword="null"/> is specified, then the call will block indefinitely until the flush operation finishes.</param>
        public WindowsAzureTableSink(string instanceName, string connectionString, string tableAddress, TimeSpan bufferInterval, int maxBufferSize, TimeSpan onCompletedTimeout)
        {
            Guard.ArgumentNotNullOrEmpty(instanceName, "instanceName");
            Guard.ArgumentNotNullOrEmpty(connectionString, "connectionString");
            Guard.ArgumentNotNullOrEmpty(tableAddress, "tableAddress");
            Guard.ArgumentIsValidTimeout(onCompletedTimeout, "onCompletedTimeout");

            this.onCompletedTimeout = onCompletedTimeout;

            CloudStorageAccount account = GetStorageAccount(connectionString);

            if (!IsValidTableName(tableAddress))
            {
                throw new ArgumentException(WindowsAzureResources.InvalidTableName, "tableAddress");
            }

            this.instanceName = NormalizeInstanceName(instanceName);
            this.client       = account.CreateCloudTableClient();
            this.client.DefaultRequestOptions = new TableRequestOptions
            {
                RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(5), 7)
            };
            this.table = this.client.GetTableReference(tableAddress);
            string sinkId = string.Format(CultureInfo.InvariantCulture, "WindowsAzureTableSink ({0})", instanceName);

            this.bufferedPublisher = BufferedEventPublisher <CloudEventEntry> .CreateAndStart(sinkId, this.PublishEventsAsync, bufferInterval, BufferCountTrigger, maxBufferSize, this.cancellationTokenSource.Token);
        }
예제 #2
0
        /// <summary>
        /// Validate the Azure connection string or throws an exception.
        /// </summary>
        /// <param name="connectionString">The connection string value.</param>
        /// <exception cref="ArgumentNullException">Null or empty connection string.</exception>
        /// <exception cref="ArgumentException">Invalid connection string.</exception>
        /// <returns>The <see cref="Microsoft.WindowsAzure.Storage.CloudStorageAccount"/>.</returns>
        private static CloudStorageAccount GetStorageAccount(string connectionString)
        {
            Guard.ArgumentNotNullOrEmpty(connectionString, "connectionString");

            try
            {
                return(CloudStorageAccount.Parse(connectionString));
            }
            catch (FormatException e)
            {
                throw new ArgumentException(Resources.InvalidConnectionStringError, "connectionString", e);
            }
        }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BufferedEventPublisher{TEntry}" /> class.
        /// </summary>
        /// <param name="sinkId">An identifier for the sink.</param>
        /// <param name="eventPublisher">The event publisher.</param>
        /// <param name="bufferingInterval">The buffering interval.</param>
        /// <param name="bufferingCount">The buffering count.</param>
        /// <param name="maxBufferSize">The maximum number of entries that can be buffered before the sink starts dropping entries.</param>
        /// <param name="cancellationToken">Cancels any pending operation.</param>
        /// <exception cref="System.ArgumentOutOfRangeException">BufferingCount out of range.</exception>
        /// <exception cref="System.ArgumentException">Argument valdation error.</exception>
        public BufferedEventPublisher(string sinkId, Func <IList <TEntry>, Task <int> > eventPublisher, TimeSpan bufferingInterval, int bufferingCount, int maxBufferSize, CancellationToken cancellationToken)
        {
            Guard.ArgumentNotNullOrEmpty(sinkId, "sinkId");
            Guard.ArgumentNotNull(eventPublisher, "eventPublisherAction");
            Guard.ArgumentGreaterOrEqualThan(500, maxBufferSize, "maxBufferSize");
            Guard.ArgumentNotNull(cancellationToken, "cancellationToken");
            Guard.ArgumentGreaterOrEqualThan(0, bufferingCount, "bufferingCount");
            Guard.ArgumentIsValidTimeout(bufferingInterval, "bufferingInterval");

            if (maxBufferSize < (bufferingCount * 3) && bufferingCount != int.MaxValue)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.MaxBufferSizeShouldBeLargerThanBufferingCount, maxBufferSize, bufferingCount), "maxBufferSize");
            }

            // throw if not auto flush parameter was supplied
            if (bufferingInterval == Timeout.InfiniteTimeSpan && bufferingCount == 0)
            {
                throw new ArgumentException(Resources.InvalidBufferingArguments);
            }

            this.sinkId         = sinkId;
            this.eventPublisher = eventPublisher;
            this.bufferingCount = bufferingCount;
            //// set minimal interval if less than default value (MinimumInterval).
            this.bufferingInterval       = (bufferingInterval < MinimumInterval && bufferingInterval != Timeout.InfiniteTimeSpan) ? MinimumInterval : bufferingInterval;
            this.maxBufferSize           = maxBufferSize;
            this.maxBatchSize            = this.bufferingCount == 0 ? this.maxBufferSize : this.bufferingCount;
            this.cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(new[] { cancellationToken });

            // defer initialization to avoid resource consumption as much as possible
            this.buffer = new Lazy <BlockingCollection <TEntry> >(() =>
            {
                this.StartBackgroundTask();
                return(new BlockingCollection <TEntry>(this.maxBufferSize));
            });
        }