Working with the Diagnostics Monitor
Updated: February 26, 2013
Author: http://msdn.microsoft.com/en-us/library/windowsazure/hh307529.aspx
Learn more about RBA Consulting.
Working with the Diagnostics Monitor
To configure the diagnostic monitor, you must access the interface provided by the DiagnosticMonitor class in the Microsoft.WindowsAzure.Diagnostics namespace. In the OnStart method of your RoleEntryPoint class, call the GetDefaultInitialConfiguration, and Start methods. To configure the diagnostic monitor, provide the diagnostic data sources, and an optional transfer interval. The previous section, "Sources of Diagnostic Data," provides detailed information about the different sources of diagnostic data, and how to enable them. The following code shows an example of how to start the diagnostic monitor with both the Windows Azure and the Windows Event logs.
public override bool OnStart()
{
var configuration = DiagnosticMonitor.GetDefaultInitialConfiguration();
// Windows Azure logs
configuration.Logs.ScheduledTransferPeriod = TimeSpan.FromSeconds(5);
configuration.Logs.ScheduledTransferLogLevelFilter = LogLevel.Undefined;
// Windows Event logs
configuration.WindowsEventLog.DataSources.Add("Application!*[System[Provider[@Name='MvcWebRole']]]");
configuration.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromSeconds(11);
configuration.WindowsEventLog.ScheduledTransferLogLevelFilter = LogLevel.Undefined;
DiagnosticMonitor.Start("DiagnosticConnectionString", configuration);
return base.OnStart();
}
The DiagnosticConnectionString string value references a configuration setting in the ServiceConfiguration.csdef file. The following code provides an example of this file. It is configured to use the storage that is provided by the development fabric.
<ServiceConfiguration ... osFamily="1" osVersion="*"> <Role name="MvcWebRole1"> <ConfigurationSettings> <Setting name="DiagnosticConnectionStringForDev" value="UseDevelopmentStorage=true" /> </ConfigurationSettings> ... </ServiceConfiguration>
Changing a Role's Diagnostic Configuration
After an application is deployed, and is running in the Windows Azure fabric, you can change a role instance's diagnostic configuration at run time without redeploying the application or recycling the role instances. This is possible because the diagnostic monitor is a separate process that polls for new changes. By default, it polls the wad-control-container blob storage at intervals of one minute. To change the diagnostic configuration at run time, use the DeploymentDiagnosticManager class in the Microsoft.WindowsAzure.Diagnostics.Management namespace. The main purpose of the DeploymentDiagnosticManager class is to provide instances of the RoleInstanceDiagnosticManager class, which represents the entry point into the configuration of each application instance.
Changing from Within an Instance
To change the configuration of a single instance from within the instance itself, use the DeploymentDiagnosticManager to retrieve the RoleInstanceDiagnosticManager of the current role. The following code shows an example of how to change the logging level of the Windows Azure logs to Information.
public ActionResult SomeMvcAction()
{
var account = CloudStorageAccount.FromConfigurationSetting("DiagnosticConnectionString");
var deploymentManager = new DeploymentDiagnosticManager(account, RoleEnvironment.DeploymentId);
var instanceManager = deploymentManager.GetRoleInstanceDiagnosticManager(
RoleEnvironment.CurrentRoleInstance.Role.Name, RoleEnvironment.CurrentRoleInstance.Id);
var configuration = instanceManager.GetCurrentConfiguration();
configuration.Logs.ScheduledTransferLogLevelFilter = LogLevel.Information;
instanceManager.SetCurrentConfiguration(configuration);
...
}
Changing All Instances
Changing the configuration of all role instances is similar to changing the configuration for a single instance. The main difference is that there is an additional loop on the collection of RoleInstanceDiagnosticManager instances that represents each instance. The following code shows a variation of the previous code example.
public ActionResult SomeMvcAction()
{
var account = CloudStorageAccount.FromConfigurationSetting("DiagnosticConnectionString");
var deploymentManager = new DeploymentDiagnosticManager(account, RoleEnvironment.DeploymentId);
var instanceManagers = deploymentManager.GetRoleInstanceDiagnosticManagersForRole(
RoleEnvironment.CurrentRoleInstance.Role.Name);
foreach (var instanceManager in instanceManagers)
{
var configuration = instanceManager.GetCurrentConfiguration();
configuration.Logs.ScheduledTransferLogLevelFilter = LogLevel.Information;
instanceManager.SetCurrentConfiguration(configuration);
}
...
}
Changing the Diagnostic Configuration Remotely
You can change a role's diagnostic configuration remotely, as well as from within the deployment. Remote access can be done with various desktop applications, tools, and services. However, in all cases, you must have an X.509 certificate to call the management API. You can generate a self-signed certificate, or use an existing one from your application. The certificate must be uploaded from the Windows Azure portal site to the management certificates store, which lists the currently uploaded certificates for each subscription. The following figure illustrates how to access the management certificates from the Windows Azure Management Portal.
The code that reconfigures a role's diagnostic configuration from a service, is similar to the code shown in the previous examples. However, instead of using the RoleEnvironment class to provide the values, you must provide the information from a different source, such as a configuration file.
You do not need to write any code to configure the diagnostic configuration from your desktop. Windows Azure provides a set of Management CmdLets that are used in Windows PowerShell. These CmdLets offer a wide variety of ways to remotely manage a Windows Azure application. For a description of the available CmdLets, see Windows Azure Management Cmdlets. To download and install the CmdLets, go to the Windows Azure Downloads page.