Alerts for exceptions in an Azure worker role

你说的曾经没有我的故事 提交于 2019-12-06 07:23:59

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.

Let me know if you have any questions. ~Cheers

I ended up using email alerts and Application insights to monitor my worker role in the Azure portal. I created an Application insight on the portal according to these instructions.

Using the Nuget package manager in Visual Studio I added the Application insights API, Application insights for website (even though my worker role is not a web app) and the Application insights trace listener.

I then created an Application insight instance by adding the following to the worker role.

private TelemetryClient tc = new TelemetryClient();

And then adding this to the onStart method.

        tc.Context.InstrumentationKey = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX";

You can find your insturmentation Key in the Azure Portal.

After running, or deploying my worker role I could then view all of my Trace.TraceInformation and TraceError statements in the Azure portal as well as add tc.TrackError and tc.TrackEvent statements to track errors and events.

TrackError worked perfectly for notifying me when an exception was thrown.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!