Пример #1
0
        public void LoadStorageAccountConfigurationSettings()
        {
            StorageAccountConfigurationSettings storageSettings = ApplicationConfiguration.Current.GetConfigurationSection <StorageAccountConfigurationSettings>(StorageAccountConfigurationSettings.SectionName);

            Assert.IsNotNull(storageSettings, "No StorageAccountConfigurationSettings section was found");
            Assert.IsFalse(storageSettings.Accounts.Count == 0, "Accounts collection is empty");
            Assert.IsFalse(String.IsNullOrEmpty(storageSettings.Accounts.Get(0).Name), "Name property is null or empty");
            Assert.IsFalse(String.IsNullOrEmpty(storageSettings.Accounts.Get(0).AccountName), "AccountName property is null or empty");
            Assert.IsFalse(String.IsNullOrEmpty(storageSettings.Accounts.Get(0).AccountKey), "AccountKey property is null or empty");

            using (MemoryStream memoryBuffer = new MemoryStream())
            {
                XmlWriterSettings writerRettings = new XmlWriterSettings();

                writerRettings.CloseOutput       = false;
                writerRettings.CheckCharacters   = false;
                writerRettings.ConformanceLevel  = ConformanceLevel.Fragment;
                writerRettings.NamespaceHandling = NamespaceHandling.OmitDuplicates;

                using (XmlWriter writer = XmlWriter.Create(memoryBuffer, writerRettings))
                {
                    storageSettings.WriteXml(writer);
                    writer.Flush();
                }

                memoryBuffer.Seek(0, SeekOrigin.Begin);

                StreamReader sr         = new StreamReader(memoryBuffer);
                string       sectionXml = sr.ReadToEnd();

                Assert.IsNotNull(sectionXml, "sectionXml is null");
            }
        }
Пример #2
0
        /// <summary>
        /// Returns the Windows Azure table storage from the specified storage account.
        /// </summary>
        /// <param name="accountName">The storage account name.</param>
        /// <returns>An instance of the object supporting storage operations against Windows Azure tables.</returns>
        public ICloudTableStorage GetTableStorage(string accountName)
        {
            Guard.ArgumentNotNull(accountName, "accountName");
            var callToken = TraceManager.WorkerRoleComponent.TraceIn(accountName);

            try
            {
                StorageAccountConfigurationSettings storageSettings = this.roleConfigExtension.GetSection <StorageAccountConfigurationSettings>(StorageAccountConfigurationSettings.SectionName);

                if (storageSettings != null)
                {
                    StorageAccountInfo storageAccount = storageSettings.Accounts.Get(accountName);

                    if (storageAccount != null)
                    {
                        return(new ReliableCloudTableStorage(storageAccount, GetStorageAccountRetryPolicy(storageAccount)));
                    }
                    else
                    {
                        throw new ConfigurationErrorsException(String.Format(CultureInfo.InvariantCulture, ExceptionMessages.StorageAccountNotFoundInConfigSource, accountName));
                    }
                }
                else
                {
                    throw new ConfigurationErrorsException(String.Format(CultureInfo.InvariantCulture, ExceptionMessages.ConfigSectionNotFoundInConfigSource, StorageAccountConfigurationSettings.SectionName));
                }
            }
            finally
            {
                TraceManager.WorkerRoleComponent.TraceOut(callToken);
            }
        }
Пример #3
0
        /// <summary>
        /// Extends the OnStart phase that is called by Windows Azure runtime to initialize the role instance.
        /// </summary>
        /// <returns>True if initialization succeeds, otherwise false.</returns>
        protected override bool OnRoleStart()
        {
            this.EnsureExists <ScalableTransformConfigurationExtension>();
            this.EnsureExists <CloudStorageProviderExtension>();
            this.EnsureExists <XslTransformMetadataProviderExtension>();
            this.EnsureExists <XslTransformInProcCacheExtension>();
            this.EnsureExists <XslTransformProviderExtension>();
            this.EnsureExists <EndpointConfigurationDiscoveryExtension>();

            IEndpointConfigurationDiscoveryExtension discoveryExtension     = Extensions.Find <IEndpointConfigurationDiscoveryExtension>();
            IRoleConfigurationSettingsExtension      roleConfigExtension    = Extensions.Find <IRoleConfigurationSettingsExtension>();
            IScalableTransformConfigurationExtension serviceConfigExtension = Extensions.Find <IScalableTransformConfigurationExtension>();

            StorageAccountConfigurationSettings storageSettings     = roleConfigExtension.GetSection <StorageAccountConfigurationSettings>(StorageAccountConfigurationSettings.SectionName);
            XslTransformCloudBlobCacheExtension cloudBlobCache      = new XslTransformCloudBlobCacheExtension(storageSettings.Accounts.Get(serviceConfigExtension.Settings.CacheStorageAccount));
            CloudStorageLoadBalancingExtension  storageLoadBalancer = new CloudStorageLoadBalancingExtension(from name in serviceConfigExtension.Settings.StorageAccounts.AllKeys select storageSettings.Accounts.Get(name));

            // Configure the cache TTL for the blob caching extension.
            cloudBlobCache.CacheTimeToLive = serviceConfigExtension.Settings.BlobCacheTimeToLive;

            // Configure the cache TTL for the in-proc caching extension.
            XslTransformInProcCacheExtension memoryCache = Extensions.Find <XslTransformInProcCacheExtension>();

            memoryCache.CacheTimeToLive = serviceConfigExtension.Settings.MemoryCacheTimeToLive;

            Extensions.Add(cloudBlobCache);
            Extensions.Add(storageLoadBalancer);

            // Done with configuring all infrastructure extensions, now proceed with registering the service extension that implements the core methods.
            this.EnsureExists <ScalableTransformServiceExtension>();

            var contractTypeMatchCondition = ServiceEndpointDiscoveryCondition.ContractTypeExactMatch(typeof(IScalableTransformationServiceContract));
            var bindingTypeMatchCondition  = ServiceEndpointDiscoveryCondition.BindingTypeExactMatch(typeof(NetTcpRelayBinding));

            discoveryExtension.RegisterDiscoveryAction(new[] { contractTypeMatchCondition, bindingTypeMatchCondition }, (endpoint) =>
            {
                NetTcpRelayBinding relayBinding = endpoint.Binding as NetTcpRelayBinding;

                if (relayBinding != null)
                {
                    relayBinding.TransferMode = TransferMode.Streamed;
                }
            });

            return(true);
        }
Пример #4
0
        private static ICloudBlobStorage GetCloudBlobStorage()
        {
            StorageAccountConfigurationSettings storageSettings = ApplicationConfiguration.Current.GetConfigurationSection <StorageAccountConfigurationSettings>(StorageAccountConfigurationSettings.SectionName);

            return(new ReliableCloudBlobStorage(storageSettings.Accounts.Get(storageSettings.DefaultQueueStorage)));
        }
Пример #5
0
        /// <summary>
        /// Notifies this extension component that it has been registered in the owner's collection of extensions.
        /// </summary>
        /// <param name="owner">The extensible owner object that aggregates this extension.</param>
        public void Attach(IExtensibleCloudServiceComponent owner)
        {
            IRoleConfigurationSettingsExtension roleConfigExtension = owner.Extensions.Find <IRoleConfigurationSettingsExtension>();

            if (roleConfigExtension != null && CloudEnvironment.IsAvailable)
            {
                ApplicationDiagnosticSettings       diagnosticSettings = roleConfigExtension.GetSection <ApplicationDiagnosticSettings>(ApplicationDiagnosticSettings.SectionName);
                StorageAccountConfigurationSettings storageSettings    = roleConfigExtension.GetSection <StorageAccountConfigurationSettings>(StorageAccountConfigurationSettings.SectionName);

                if (diagnosticSettings != null)
                {
                    if (diagnosticSettings.DiagnosticEnabled)
                    {
                        DiagnosticMonitorConfiguration diagnosticConfig = DiagnosticMonitor.GetDefaultInitialConfiguration();

                        // Configure the scheduled transfer period for all logs.
                        diagnosticConfig.DiagnosticInfrastructureLogs.ScheduledTransferPeriod = diagnosticSettings.DiagnosticLogsTransferPeriod.Coalesce(diagnosticSettings.DefaultTransferPeriod);
                        diagnosticConfig.Directories.ScheduledTransferPeriod         = diagnosticSettings.FileLogsTransferPeriod.Coalesce(diagnosticSettings.DefaultTransferPeriod);
                        diagnosticConfig.Logs.ScheduledTransferPeriod                = diagnosticSettings.TraceLogsTransferPeriod.Coalesce(diagnosticSettings.DefaultTransferPeriod);
                        diagnosticConfig.PerformanceCounters.ScheduledTransferPeriod = diagnosticSettings.PerformanceCountersTransferPeriod.Coalesce(diagnosticSettings.DefaultTransferPeriod);
                        diagnosticConfig.WindowsEventLog.ScheduledTransferPeriod     = diagnosticSettings.EventLogsTransferPeriod.Coalesce(diagnosticSettings.DefaultTransferPeriod);

                        // Configure the logs levels for scheduled transfers.
                        diagnosticConfig.DiagnosticInfrastructureLogs.ScheduledTransferLogLevelFilter = FromTraceSourceLevel(diagnosticSettings.DiagnosticLogsTransferFilter);
                        diagnosticConfig.Logs.ScheduledTransferLogLevelFilter            = FromTraceSourceLevel(diagnosticSettings.TraceLogsTransferFilter);
                        diagnosticConfig.WindowsEventLog.ScheduledTransferLogLevelFilter = FromTraceSourceLevel(diagnosticSettings.EventLogsTransferFilter);

                        // Configure the Windows Event Log data sources.
                        foreach (string logName in diagnosticSettings.EventLogDataSources.AllKeys)
                        {
                            diagnosticConfig.WindowsEventLog.DataSources.Add(logName);
                        }

                        // Configure the data sources for file-based logs.
                        foreach (string containerName in diagnosticSettings.FileLogDirectories.AllKeys)
                        {
                            diagnosticConfig.Directories.DataSources.Add(new DirectoryConfiguration()
                            {
                                Container = containerName, Path = diagnosticSettings.FileLogDirectories[containerName].Value
                            });
                        }

                        // Configure the data sources for performance counter data
                        foreach (string counterName in diagnosticSettings.PerformanceCountersDataSources.AllKeys)
                        {
                            diagnosticConfig.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration()
                            {
                                CounterSpecifier = counterName, SampleRate = TimeSpan.Parse(diagnosticSettings.PerformanceCountersDataSources[counterName].Value)
                            });
                        }

                        // Configure crash dumps collection.
                        if (diagnosticSettings.CrashDumpCollectionEnabled)
                        {
                            CrashDumps.EnableCollection(true);
                        }

                        // Look up for the storage account definition.
                        StorageAccountInfo storageAccountInfo = storageSettings.Accounts.Get(diagnosticSettings.DiagnosticStorageAccount);

                        if (storageAccountInfo != null)
                        {
                            CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentialsAccountAndKey(storageAccountInfo.AccountName, storageAccountInfo.AccountKey), true);
                            RetryPolicy         retryPolicy    = roleConfigExtension.StorageRetryPolicy;

                            // Start the Azure Diagnostic Monitor using a retryable scope.
                            this.diagnosticMonitor = retryPolicy.ExecuteAction <DiagnosticMonitor>(() => { return(DiagnosticMonitor.Start(storageAccount, diagnosticConfig)); });
                        }
                    }
                    else
                    {
                        // Do not proceed any further since diagnostic is not enabled in the application configuration.
                        return;
                    }
                }
            }

            if (null == this.diagnosticMonitor)
            {
                // Configuration extension is not available by some reasons, let try and see if DiagnosticsConnectionString property is set in the configuration.
                string diagConnectionString = CloudEnvironment.GetConfigurationSettingValue(Resources.DiagnosticsConnectionStringSettingName);

                // If DiagnosticsConnectionString is defined, start a Diagnostic Monitor using the storage account configuration specified in the setting.
                if (!String.IsNullOrEmpty(diagConnectionString))
                {
                    this.diagnosticMonitor = DiagnosticMonitor.Start(diagConnectionString, GetDiagnosticMonitorDefaultConfiguration());
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Initializes a new instance of the trace listener using the specified configuration settings.
        /// </summary>
        /// <param name="diagnosticServiceEndpoint">The name of the Service Bus endpoint definition that corresponds to the on-premises hosted diagnostic service.</param>
        /// <param name="diagnosticStorageAccount">The name of the storage account in which diagnostic data will be queued before relayed to the on-premises hosted diagnostic service.</param>
        /// <param name="inMemoryQueueCapacity">The maximum capacity of the non-durable in-memory queue in which trace events are being stored before they are persisted into an Azure queue.</param>
        /// <param name="inMemoryQueueListenerSleepTimeout">The interval in milliseconds during which a dequeue thread is waiting for new events in the in-memory queue.</param>
        /// <param name="diagQueueEventBatchSize">The maximum number of trace events in a batch that is submitted to the on-premises hosted diagnostic service.</param>
        /// <param name="diagQueueListenerSleepTimeout">The interval in milliseconds during which a dequeue thread is waiting for new events to be deposited into the Azure queue.</param>
        public OnPremisesBufferedTraceListener(string diagnosticServiceEndpoint, string diagnosticStorageAccount, int inMemoryQueueCapacity, int inMemoryQueueListenerSleepTimeout, int diagQueueEventBatchSize, int diagQueueListenerSleepTimeout)
        {
            Guard.ArgumentNotNullOrEmptyString(diagnosticServiceEndpoint, "diagnosticServiceEndpoint");
            Guard.ArgumentNotZeroOrNegativeValue(inMemoryQueueCapacity, "inMemoryQueueCapacity");
            Guard.ArgumentNotZeroOrNegativeValue(inMemoryQueueListenerSleepTimeout, "inMemoryQueueListenerSleepTimeout");
            Guard.ArgumentNotZeroOrNegativeValue(diagQueueEventBatchSize, "diagQueueEventBatchSize");
            Guard.ArgumentNotZeroOrNegativeValue(diagQueueListenerSleepTimeout, "diagQueueListenerSleepTimeout");

            // Configure the general properties of the trace listener.
            Name       = ListenerName;
            NeedIndent = false;

            // Configure the in-memory queue and its parameters, set up a backup trace listener.
            this.inMemoryQueue                     = new ConcurrentQueue <TraceEventRecord>();
            this.inMemoryQueueCapacity             = inMemoryQueueCapacity;
            this.inMemoryQueueListenerSleepTimeout = inMemoryQueueListenerSleepTimeout;
            this.diagQueueEventBatchSize           = diagQueueEventBatchSize;
            this.diagQueueListenerSleepTimeout     = diagQueueListenerSleepTimeout;
            this.diagQueueName                     = GetServiceBusQueueName(CloudEnvironment.CurrentRoleInstanceId);
            this.backupTraceListener               = new CloudDiagnosticTraceListener();

            // Configure Service Bus endpoint for the Diagnostic Service.
            ServiceBusEndpointInfo diagServiceEndpointInfo = ApplicationConfiguration.Current.ServiceBusSettings.Endpoints[diagnosticServiceEndpoint];

            // Validate Service Bus endpoints.
            if (null == diagServiceEndpointInfo)
            {
                throw new ConfigurationErrorsException(String.Format(CultureInfo.CurrentCulture, ExceptionMessages.SpecifiedServiceBusEndpointNotFound, diagnosticServiceEndpoint, ServiceBusConfigurationSettings.SectionName));
            }

            // Retrieve the storage account settings from application configuration.
            StorageAccountConfigurationSettings storageSettings = ApplicationConfiguration.Current.GetConfigurationSection <StorageAccountConfigurationSettings>(StorageAccountConfigurationSettings.SectionName);

            // Validate the presence of storage account settings.
            if (null == storageSettings)
            {
                throw new ConfigurationErrorsException(String.Format(CultureInfo.CurrentCulture, ExceptionMessages.ConfigSectionNotFoundInConfigSource, StorageAccountConfigurationSettings.SectionName));
            }

            // Determine the storage account for storing tracing data. By default, assume that storage account is defined in the trace listener configuration.
            string diagStorageAccountName = diagnosticStorageAccount;

            // If storage account is not defined in the trace listener configuration, switch to the storage account specified in the Application Diagnostic Settings section.
            if (String.IsNullOrEmpty(diagStorageAccountName))
            {
                // Retrieve the diagnostic settings from application configuration.
                ApplicationDiagnosticSettings diagSettings = ApplicationConfiguration.Current.GetConfigurationSection <ApplicationDiagnosticSettings>(ApplicationDiagnosticSettings.SectionName);

                // Validate the presence of diagnostic settings.
                if (null == diagSettings)
                {
                    throw new ConfigurationErrorsException(String.Format(CultureInfo.CurrentCulture, ExceptionMessages.ConfigSectionNotFoundInConfigSource, ApplicationDiagnosticSettings.SectionName));
                }

                diagStorageAccountName = diagSettings.DiagnosticStorageAccount;
            }

            // Resolve the storage account details based on the account name defined in the diagnostic settings .
            StorageAccountInfo diagStorageAccount = storageSettings.Accounts.Get(diagStorageAccountName);

            // Validate storage account details.
            if (null == diagStorageAccount)
            {
                throw new ConfigurationErrorsException(String.Format(CultureInfo.CurrentCulture, ExceptionMessages.StorageAccountNotFoundInConfigSource, diagStorageAccountName));
            }

            // Configure retry policies.
            this.sbRetryPolicy                   = new RetryPolicy <ServiceBusTransientErrorDetectionStrategy>(DefaultRetryCount);
            this.queueRetryPolicy                = new RetryPolicy <StorageTransientErrorDetectionStrategy>(DefaultRetryCount);
            this.sbRetryPolicy.RetryOccurred    += HandleServiceBusEndpointRetryState;
            this.queueRetryPolicy.RetryOccurred += HandleCloudQueueRetryState;

            // Configure Service Bus and Message Queue clients.
            this.diagnosticService   = new ReliableServiceBusClient <IDiagnosticLoggingServiceChannel>(diagServiceEndpointInfo, this.sbRetryPolicy);
            this.diagQueueStorage    = new ReliableCloudQueueStorage(diagStorageAccount, this.queueRetryPolicy);
            this.diagQueueReaderDone = new ManualResetEvent(false);
            this.diagQueueWriterDone = new ManualResetEvent(false);

            // Ensure that destination cloud queue exists, create if necessary.
            this.diagQueueStorage.CreateQueue(this.diagQueueName);

            // Enable background listeners to run.
            this.isRunning = true;

            StartInMemoryQueueListener();
            StartServiceBusQueueListener();
        }