Windows Azure SQL Database Marketplace
Editors Note: Today's post comes from Rick Anderson, ASP.NET MVC Programmer/Writer. He discusses using Trace in Windows Azure cloud apps.
Most of the sample code for Windows Azure uses the Trace API. When you create a new Windows Azure Cloud application in Visual Studio (and you have installed the Windows Azure SDK for Visual Studio ), the created application contains the Trace code:
The Windows Azure template generated code has the trace diagnostics listener partially configured, this post will show you how to fully configure tracing.
I like to use Trace.TraceError for errors and Trace.TraceInformation for information.
But calling the Trace API is not enough to actually persist the data in Azure. You need to configure the diagnostic monitor. I use the following code to do that:
private void ConfigDiagnostics()
DiagnosticMonitorConfiguration config =
config.ConfigurationChangePollInterval = TimeSpan.FromMinutes(1d);
config.Logs.BufferQuotaInMB = 500;
config.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
config.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1d);
In the code above, the transfer rate is set to one minute (which is the minimum, anything lower will be rounded up to one minute). If you application generates trace data very slowly, say one event per minute, only one row will be uploaded from each agent each minute in a separate transaction. By setting the upload rate to 60 minutes, then 60 rows could be packaged into a single batch and uploaded in one transaction -- you would be saving on transaction costs. The downside of using a large transfer period:
A large delay in data showing up in storage.
If there is a lot of data (dozens of thousands, or hundreds of thousands) of rows, then the upload will be bursty (as opposed to smoothed over) and hence can cause throttling.
The sample sets the ScheduledTransferLogLevelFilter to Verbose, so all trace messages are captured.
To use the code, in the OnStart() method of each web role and worker role, I call ConfigDiagnostics as shown in the following code:
public override bool OnStart()
Trace.TraceInformation("Initializing storage account")
// Code removed for clarity.
Note: The above code works only in Windows Azure Cloud Applications, it won’t work in Windows Azure Web Sites.
Warning: Storing Trace information my incur a charge. See the Windows Azure Pricing Calculator for details. You can view trace data with Azure Storage Explorer under the WADLogsTable.
A best practice is to use a separate storage account for trace data and production data. The following XML from my ServiceConfiguration.Cloud.cscfg file shows the Trace configuration.
<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="AzureEmailService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="1" osVersion="*" schemaVersion="2012-05.1.7">
<Instances count="1" />
Tracing provides a great way to monitor your Windows Azure application and provide rich information to help debug problems.
For more information on Tracing in Windows Azure, see the following: