1
votes

I have scheduled my console application as a web job in azure portal.

how to get azure web job running alert in mail from azure portal that whether it runs successfully or any failure occurred.

2

2 Answers

0
votes

how to get azure web job running alert in mail from azure portal that whether it runs successfully or any failure occurred.

I haven't found any way to do it on Azure portal. You could modify your code to implement it.

As one of my original posts mentioned, WebJobs status depends on whether your WebJob/Function is executed without any exceptions or not. I suggest you put all your code in a try code block. If any exception throws, it means the status of this run will be failure. Otherwise the status will be success.

try
{
    //Put all your code here
    //If no exception throws, the status of this run will be success
    //Send success status to your mail
}
catch
{
    //If any exception throws, the status of this run will be failure
    //Send failure status to your mail
}

To send a mail in your WebJob, you could use SendGrid component or any other SMTP client library. Here is a sample of using SmtpClient to send mail from hotmail.

MailMessage mail = new MailMessage("[email protected]", "[email protected]");
SmtpClient client = new SmtpClient();
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new NetworkCredential("[email protected]", "mail_password");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "smtp.live.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
0
votes

I wasn't able to find a way to send emails on job run or even failure using only "pure" azure tools (without me writing any code). However, what you can do is use Kudu Web Hooks for job completion: https://github.com/projectkudu/kudu/wiki/Web-hooks (Kudu is part of Azure).

For example, you can create another web job or a function with an HTTP trigger which will run on each web hook call, will check job completion status, and will send emails on failure. Thus, you can have single web job handling errors of all other web jobs you have.

Web UI for configuring web hooks is at https://your_web_app_name.scm.azurewebsites.net/WebHooks, and you can find the model of the web hook message body here: https://github.com/projectkudu/kudu/blob/master/Kudu.Contracts/Jobs/TriggeredJobRun.cs.