Click here to Skip to main content
Click here to Skip to main content
Go to top

Windows Azure Event Logs Notifier

, 8 Aug 2012
Rate this:
Please Sign up or sign in to vote.
Create an email notifier using windows azure event error logs

Introduction

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.

Problem explained

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.

The solution

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:

  1. Collect Windows event logs using Windows Azure Diagnostics (Microsoft.WindowsAzure.Diagnostics.Management namespace)
  2. Retrieve log errors from WADWindowsEventLogsTable
  3. Send email notification

Collect Windows event logs using Windows Azure Diagnostics

Remote configuring Windows event logs
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics.Management;

// fetch from the windows azure portal
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(
                        String.Format(
                        "DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",  
                        storageAccoutName, privateKey));

// Get the diagnostics manager for the entire storage account.
var diagnosticManager = storageAccount.CreateDeploymentDiagnosticManager(deploymentId);

// Get diagnostics manager for the specific role instance.
RoleInstanceDiagnosticManager roleDiagManager = 
  diagnosticManager.GetRoleInstanceDiagnosticManager(roleName, roleInstanceName);

//Modify current configuration
var currentConfiguariton = roleDiagManager.GetCurrentConfiguration();

// Collect only application event error logs
currentConfiguariton.WindowsEventLog.DataSources.Add("Application!*");
currentConfiguariton.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
currentConfiguariton.WindowsEventLog.ScheduledTransferLogLevelFilter = Microsoft.WindowsAzure.Diagnostics.LogLevel.Error;

//Commit the changes
roleDiagManager.SetCurrentConfiguration(currentConfiguariton);

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 WADWindowsEventLogsTable.

326949/Capture.PNG

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 = 
       new EventLogDataContext(accountStorage.TableEndpoint.ToString(), 
       accountStorage.Credentials);
    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
             select d).AsTableServiceQuery<EventLogData>();

    List<EventLogData> selectedData = new List<EventLogData>();
    try
    {
        selectedData = query.Execute().ToList<EventLogData>();
    }
    catch
    {
    }
    return selectedData;
} 
Two important columns to focus on in WADWindowsEventLogsTable are the ProviderName column which is the application source of the error and the Description column which is the error message.

Send email notification

In order to send mails, I used the 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)
{
    try
    {
        MailMessage msg = new MailMessage()
        {
            Subject = messageSubject,
            From = new MailAddress(emailConfig.From)
        };

        msg.To.Add(new MailAddress(emailConfig.To));
        msg.Body = messageBody;

        using (SmtpClient smpt = new SmtpClient()
        {
            Host = emailConfig.SMTPHost,
            Port = 25,
            EnableSsl = true,
            Credentials = new NetworkCredential(emailConfig.UserName, emailConfig.Password)
        })
        {
            smpt.Send(msg);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

Just for testing I've created a small WinForms application (the UI code is omitted from the source code).

326949/Capture1.PNG

The following is the email message I received:

326949/Capture2.PNG

Summary

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 Azure application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shay Yannay
Software Developer Quest Software
Israel Israel
Shay Yannay is a Senior Software Developer and Windows Azure Domain Expert.
He is experienced with designing and developing highly scalable, distributed, 24x7 availability complex system. Shay also specializes in performance management & diagnostics of multi-tier applications.
He is passionate about the cloud technologies and trends, specifically with Microsoft Azure.
He currently works for Dell as an Azure Specialist.
 
Shay holds a B.Sc in Communication Systems Engineering from the Ben-Gurion university.
 
Personal Blog: http://shayyannay.wordpress.com
Follow on   Twitter

Comments and Discussions

 
QuestionNice Article, but How to get deploymentId ? Pinmembershriji.111123-Apr-13 0:47 
AnswerRe: Nice Article, but How to get deploymentId ? PinmemberShay Yannay23-Apr-13 1:30 
GeneralMy vote of 5 PinmvpKanasz Robert28-Sep-12 5:36 
GeneralMy vote of 5 Pinmembermanoj kumar choubey25-Apr-12 21:02 
GeneralMy vote of 5 PinmemberNavaVa13-Feb-12 22:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 8 Aug 2012
Article Copyright 2012 by Shay Yannay
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid