Monitoring Azure applications is no longer the developer's sole
responsibility. Often times the devOp is actually the one who is in charge of the stability
and functionality of the cloud application.
The Windows Azure platform provides a great diagnostic mechanism
called Windows Azure Diagnostics (check this
article on more info).
As you may know an Azure application's main component is
the virtual machine that actually runs the code. Let's go over Windows Azure Roles types very quickly: We have the Web role which is the web site hosting the IIS server, and the Worker
role which execute tasks periodically.
As an operator of the application I would like to be notified
immediately when an error occurs. Responding quickly is very crucial. Once notified I can start debugging the application and identify the root cause.
I already know that Windows Azure Diagnostics can
collect Windows events from my role instances. All I need to do is to analyze the data at
a specific sampling
rate and send some sort of notification, preferably an email, to the person in
charge. Thus aiming for very fast problem resolution.
In the code below I will demonstrate how you can create a basic error notification mechanism.
Using the code
Let’s review the tasks we need to achieve:
Windows event logs using Windows Azure Diagnostics (
- Retrieve log errors from WADWindowsEventLogsTable
- Send email notification
Collect Windows event logs using Windows Azure Diagnostics
Windows event logs
const string storageAccoutName = "INSERT-AZURE-STORAGE-NAME";
const string privateKey = "INSERT-STORAGE-PRIVATE-KEY";
const string deploymentId = "INSERT-DEPLOYMENT-ID";
const string roleName = "INSERT-ROLE-NAME";
const string roleInstanceName = "INSERT-ROLE-INSTANCE";
var storageAccount = CloudStorageAccount.Parse(
var diagnosticManager = storageAccount.CreateDeploymentDiagnosticManager(deploymentId);
RoleInstanceDiagnosticManager roleDiagManager =
var currentConfiguariton = roleDiagManager.GetCurrentConfiguration();
currentConfiguariton.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
currentConfiguariton.WindowsEventLog.ScheduledTransferLogLevelFilter = Microsoft.WindowsAzure.Diagnostics.LogLevel.Error;
The code above starts the persistence of Windows event logs to the
Azure storage. I used Cloud Storage Manager to verify that logs are written to
Retrieve log errors from WADWindowsEventLogsTable
In the following code I'm constructing a LINQ query to WADWindowsEventLogsTable and fetching only the error logs. I can call this method periodically (using some sort of timer) to be acknowledged on new errors in my application.
public List<EventLogData> QueryEventLogData(LogLevel logLevel, string deploymentid, string roleName,
string roleInstanceName, DateTime startPeriod, DateTime endPeriod)
EventLogDataContext context =
var data = context.PerfData;
CloudTableQuery<EventLogData> query = null;
query = (from d in data
where d.PartitionKey.CompareTo("0" + startPeriod.Ticks) >= 0
&& d.PartitionKey.CompareTo("0" + endPeriod.Ticks) <= 0
&& d.Level == (int)logLevel
&& d.EventTickCount >= startPeriod.Ticks
&& d.EventTickCount <= endPeriod.Ticks
&& d.DeploymentId == deploymentid
&& d.Role == roleName
&& d.RoleInstance == roleInstanceName
List<EventLogData> selectedData = new List<EventLogData>();
selectedData = query.Execute().ToList<EventLogData>();
Two important columns to focus on in WADWindowsEventLogsTable are the
column which is the application source of the error and
column which is the error message.
Send email notification
In order to send mails, I used
System.Net.Mail namespace. I configured it to send via SMTP from my
GMail account. But it can be easily configured to use other emailing services as well.
public static void sendEmail(string messageSubject, string messageBody, EmailClientConfiguration emailConfig)
MailMessage msg = new MailMessage()
Subject = messageSubject,
From = new MailAddress(emailConfig.From)
msg.Body = messageBody;
using (SmtpClient smpt = new SmtpClient()
Host = emailConfig.SMTPHost,
Port = 25,
EnableSsl = true,
Credentials = new NetworkCredential(emailConfig.UserName, emailConfig.Password)
catch (Exception ex)
Just for testing I've created a small WinForms application (the UI code is omitted from the source code).
The following is the email message I received:
In this article I demonstrated how you can address critical monitoring tasks by focusing on specific diagnostics data. You can leverage this solution to other use cases which applies to your