Click here to Skip to main content
Licence CPOL
First Posted 9 Feb 2012
Views 7,772
Bookmarked 5 times

Windows Azure Event Logs Notifier

By | 9 Feb 2012 | Article
Create an email notifier using windows azure event error logs

Introduction

Monitoring Azure application is no longer the developer's sole responsibility.
Many times the devOp is actually the one who is in charge on the stability and functionality of the cloud application.

The Windows Azure platform provides a great diagnostic mechanism called Windows Azure Diagnostics (check the following article on more info).

As you may know the azure application main components are the virtual machine that actually runs the code.

Lets 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 specific sampling rate and send some sort of notification, preferably an email, to the person in charge.
Thus aiming for problem resolution very fast.

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

Capture.PNG

Retrieve log errors from the 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 Description 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)
{
    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 winfrom application (the UI code is omitted from the source code).

Capture1.PNG

The following is the email message I've received

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 you azure application.

Download ErrorNotifier.zip - 42.71 KB

Points of Interest

In Quest Software we developed a very handy performance monitoring tool named Spotlight on Azure that enables you to easily control azure resources and isolate performance problems in your applications from the role instance up to the azure subscription account.
Follow us on QuestCloudTools.com

License

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

About the Author

Shay Yannay

Software Developer
Quest Software
Israel Israel

Member

Follow on Twitter Follow on Twitter
Shay Yannay is a Software Developer and Technology Evangelist.
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 Quest Software's cloud tools division as an Azure Specialist.
 
Shay holds a B.Sc in Communication Systems Engineering from the Ben-Gurion university.
 
Personal Blog: http://shayyannay.wordpress.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey21:02 25 Apr '12  
GeneralMy vote of 5 PinmemberNavaVa22:19 13 Feb '12  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 9 Feb 2012
Article Copyright 2012 by Shay Yannay
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid