Getting Started with Windows Azure Diagnostics and Monitoring
The Windows Azure diagnostics service runs side-by-side with your role instance and collects diagnostics data as dictated by the configuration. The diagnostics service saves the data to your Windows Azure storage service if it is configured to do so. The diagnostics service can also be communicated with remotely from on-premise apps (a good example of this is the Windows Azure Diagnostics Manager). The Azure diagnostics service supports monitoring and logging of the following information from the Azure service:
- Windows Azure logs: The application logs dumped from an application which can be any messages emitted by the app.
- Diagnostic monitor logs: Logs about the running of the diagnostics service.
- Windows performance counters: A range of performance metrics such as processor utilization, response times etc
- Windows event logs: Logs generated on the instance where the role instance is running.
- IIS logs and failed request traces: IIS logs and IIS failed request traces generated by a Web role instance.
- Application crash dumps: Crash dumps generated upon an app crash.
To gather diagnostics in Windows Azure there are two steps – configuration and management. First you will need to configure the diagnostics service with the data types you wish to collect. Then you will need to use the diagnostics management API which comes with the Windows Azure SDK you can perform regularly scheduled or on-demand transfers of the diagnostics data from the role instances to a Windows Azure storage account. The diagnostics management API also allows for changing the configuration of an already running diagnostics service.
For the purposes of this article we will demonstrate how to use Windows event logs.
To use Windows Azure diagnostics with you cloud service you need to follow three steps:
- Configure the trace listener.
- Define the storage location for the diagnostics service.
- Start the Windows Azure diagnostics service.
Configuring the Trace Listener
When a new role is created in Visual Studio an app.config (for a Worker Role) or web.config (for WCF and Web roles) files is created automatically in the role project and the trace listener provider is included by default:
<add type=”Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=18.104.22.168, Culture=neutral, PublicKeyToken=31bf3856ad364e35″
<filter type=”" />
The DiagnosticMonitorTraceListener enables the use of the .NET Tracing API for logging within your code. For example, the Write() and WriteLine() methods of the System.Diagnostics.Trace class can perform logging operations anywhere within your code:
Trace.WriteLine(“INFORMATION LOG”, “Information”);
Trace.WriteLine(“CRITICAL LOG”, “Critical”);
Defining a Storage Location for the Diagnostics Service
In the ServiceDefinition.csdef and ServiceConfiguration.cscfg files, you are required to define the diagnostics connection string for the designated storage location for the logging data. Visual Studio generates the configuration automatically (ie can also be modified using the configuration UI by double-clicking on the relevant role in the Roles folder under the Cloud Service).
If the configuration is using the local storage it will look as below:
<Setting name=”DiagnosticsConnectionString” value=”UseDevelopmentStorage=true” />
Alternatively it can be configured to store the data on an Azure storage account:
<Setting name=”DiagnosticsConnectionString” value=
Starting the Windows Azure Diagnostics Service
Now you have configured your app to use logging and defined a storage location, all that is required is to start the diagnostics service. To do this pass in the connection string name you defined above.
If the role was created in Visual Studio the WebRole.cs/WorkerRole.cs files already contain the code for starting the diagnostics service in the OnStart() method – ie DiagnosticMonitor.Start(“DiagnosticsConnectionString”);.
Once started, the diagnostics monitoring service will start collecting logged data. The diagnostics service can be further configured using the DiagnosticMonitorConfiguration class:
//Get the configuration object
DiagnosticMonitorConfiguration diagObj= DiagnosticMonitor.GetDefaultInitialConfiguration();
//Set the service to transfer logs every 15 mins to the storage account
diagObj.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(15);
//Start Diagnostics Monitor with the new storage account configuration
The above code configured the diagnostics monitor to transfer logs to the storage account every 15 minutes.