首页 文章

Azure工作者角色中的异常警报

提问于
浏览
2

如果工作者角色抛出异常或出现错误,是否有一种简单的方法可以在Azure管理门户中发送警报或通知?

我使用的是Azure 2.5

我已经设置了跟踪和诊断,并且可以在Visual Studio的服务器资源管理器中查看日志,但是如果例如并且日志中出现错误消息,则无论如何都要设置警报 .

我知道您可以在管理门户中设置监控指标的警报是否有一种简单的方法来添加错误和异常的指标?

或者在某种程度上获取C#异常代码以在Azure管理门户中创建通知或警报?

2 回答

  • 3

    我使用SendGrid通过Azure发送电子邮件,因为它是免费的 . 这是我会做的事情如下:

    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);
    }
    

    请注意,我将我的信用存储在名为ServerGlobalVariables的globalvariables类中 . 此外,我发送格式为HTML的电子邮件,但您不必这样做 .

    如果您有任何疑问,请告诉我 . 〜干杯

  • 1

    我最终使用电子邮件警报和应用程序洞察来监视Azure门户中的工作者角色 . 我根据these instructions在门户上创建了应用程序洞察 .

    在Visual Studio中使用Nuget包管理器,我添加了Application insights API,网站的应用程序洞察(即使我的工作者角色不是Web应用程序)和Application insights跟踪侦听器 .

    然后,我通过将以下内容添加到辅助角色来创建应用程序洞察实例 .

    private TelemetryClient tc = new TelemetryClient();
    

    然后将其添加到onStart方法中 .

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

    您可以在Azure门户中找到您的insturmentation Key .

    运行或部署我的辅助角色后,我可以在Azure门户中查看我的所有Trace.TraceInformation和TraceError语句,并添加tc.TrackError和tc.TrackEvent语句来跟踪错误和事件 .

    TrackError非常适合在抛出异常时通知我 .

相关问题