azureazure-storageazure-worker-rolesazure-diagnostics

Alerts for exceptions in an Azure worker role


Is there an easy way to send an alert or notification in the Azure Management portal if a worker role throws an exception or has an error?

I am using Azure 2.5

I have tracing and diagnostics all set up and can view the logs in the server explorer of Visual studio, but is there anyway to set an alert if for example and error message appears in the logs.

I know you can set up alerts for monitoring metrics in the Management portal is there an easy way to add metrics for errors and exceptions?

Or someway to get C# exception code to create notifications or alerts in the Azure Management portal?


Solution

  • I use SendGrid for sending emails through Azure because it's free. Here is what I would do something like below:

    try
    {
         //....
    }
    catch(Exception ex)
    {
         MailMessage mailMsg = new MailMessage() {
                    //Set your properties here
                };
        
         // Add the alternate body to the message.
         mailMsg.AlternateViews.Add(
                AlternateView.CreateAlternateViewFromString(Body
                       , new System.Net.Mime.ContentType("text/html")));
    
         SmtpClient smtpClient = new SmtpClient(
                                     ServerGlobalVariables.SmtpServerHost
                                     , Convert.ToInt32(587));
    
         System.Net.NetworkCredential credentials = 
              new System.Net.NetworkCredential(
                     ServerGlobalVariables.SmtpServerUserName
                     , ServerGlobalVariables.SmtpServerPassword);
    
         smtpClient.Credentials = credentials;
    
         smtpClient.Send(mailMsg);
    }
    

    Please note that I store my credits in a globalvariables class called ServerGlobalVariables. Also, I send my emails formatted as HTML, but you don't have to do that.