public void ValidateApplicationDiagnosticSettings() { ApplicationConfiguration appConfig = ApplicationConfiguration.Current; Assert.IsTrue(ApplicationConfiguration.IsLoaded); ApplicationDiagnosticSettings loggingSettings = appConfig.GetConfigurationSection <ApplicationDiagnosticSettings>(ApplicationDiagnosticSettings.SectionName); //loggingSettings.AddEventLogDataSource("Application!*"); //loggingSettings.AddEventLogDataSource("System!*"); //loggingSettings.AddFileLogDirectory("blob1", @"C:\Program Files\"); //loggingSettings.AddFileLogDirectory("blob2", @"C:\Windows\Logs\"); //loggingSettings.AddPerformanceCounter(@"\Processor(*)\% Processor Time", TimeSpan.FromSeconds(30)); //loggingSettings.AddPerformanceCounter(@"\Memory\Available Mbytes", TimeSpan.FromSeconds(120)); Assert.IsNotNull(loggingSettings, "No ApplicationDiagnosticSettings section was found"); StringWriter sw = new StringWriter(); XmlTextWriter xmlWriter = new XmlTextWriter(sw); loggingSettings.WriteXml(xmlWriter); string configSectionText = sw.GetStringBuilder().ToString(); Assert.IsTrue(configSectionText.Length > 0); }
private void ConfigureDefaults() { IRoleConfigurationSettingsExtension roleConfigExtension = Extensions.Find <IRoleConfigurationSettingsExtension>(); ApplicationDiagnosticSettings diagnosticSettings = roleConfigExtension.GetSection <ApplicationDiagnosticSettings>(ApplicationDiagnosticSettings.SectionName); // If heartbeat interval is defined in the application diagnostic settings, override the default interval for this role. if (diagnosticSettings != null && diagnosticSettings.HeartbeatInterval.TotalMilliseconds > 0) { HeartbeatInterval = diagnosticSettings.HeartbeatInterval; } }
/// <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()); } } }
/// <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(); }