示例#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);
        }
        /// <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);
            }
        }