• Windows Azure入门教学系列 (九):Windows Azure 诊断功能


    公告 :本博客为微软云计算中文博客 的镜像博客。 部分文章因为博客兼容性问题 ,会影响阅读体验 。如遇此情况,请访问 原博客

    05.aspx

    本文是 Windows Azure入门教学的第九篇文章。

    本文将会介绍如何使用 Windows Azure  诊断功能。跟部署在本地服务器上的程序不同,当我们的程序发布到云端之后,我们不能使用通常的调试方法,例如 Remote Debugging等等来对我们的程序进行调试。那么当程序运行出现问题时我们如何能够得知问题的根源以便修正 Bug呢?如果我要检测程序的性能,又有什么好的方法呢?

    要做到上述的需求,我们需要利用 Windows Azure提供的诊断功能,该功能能够记录诊断日志,保存到 Windows Azure Storage上。如果要检测程序的性能可以使用性能计数器。该例子不在本文范围内。本文将引导读者建立一个简单的 Windows Azure Web Role,  在页面加载时记录一段信息,并从 Table Storage中观察收集到的信息。

    在开始本教学之前,请确保你从 Windows Azure  平台下载 下载并安装了最新的 Windows Azure开发工具。 本教学使用 Visual Studio 2010作为开发工具。

    步骤一:创建解决方案和项目

    启动 Visual Studio 2010,新建一个 Cloud Service项目并为之添加一个 Web Role

    步骤二:添加代码

    Default.aspx.cs中添加下列代码 :

    protected   void   Page_Load(object   sender,  EventArgs   e)

    {

         System.Diagnostics.Trace .WriteLine("Page_Load is called" ,  "Information" );

    }

     

    WebRole.cs中添加下列代码:

     

    public   override   bool   OnStart()

    {

         //   获取用于 Windows Azure 诊断的默认初始配置

         DiagnosticMonitorConfiguration   diagConfig =  DiagnosticMonitor .GetDefaultInitialConfiguration();

         diagConfig.Logs.ScheduledTransferLogLevelFilter =  LogLevel .Verbose;

     

         //  制定预定传输间隔

         diagConfig.Logs.ScheduledTransferPeriod = System.TimeSpan .FromMinutes(1);

     

         //  制定预定传输间隔

         DiagnosticMonitor .Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" , diagConfig);

     

         // For information on handling configuration changes

         // see the MSDN topic at  http://go.microsoft.com/fwlink/?LinkId=166357 .

     

         return   base .OnStart();

    }

     

    步骤三:观察并分析代码

    我们需要关心的代码主要有:

    ·          diagConfig.Logs.ScheduledTransferLogLevelFilter =  LogLevel .Verbose;  

    该代码设置了日志过滤级别。有些情况下我们只关心严重的错误,这时候我们可以设置过滤级别为 LogLevel.Error或者 LogLevel.Critical。这样只有符合条件的日志才会被传输到 cloud storage上。

    ·          diagConfig.Logs.ScheduledTransferPeriod = System.TimeSpan .FromMinutes(1);

    这段代码设置了传输间隔。这里我们设置为每隔一分钟传输一次日志。此外我们还可以通过手动传输日志。详情请参考 传输诊断数据到 Windows Azure Storage

    另外需要指出的是,要收集跟踪信息我们需要在 Web.config中添加:

    < system.diagnostics >

    < trace >

         < listeners >

         < add   type = "Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 "  name = "AzureDiagnostics ">

             < filter   type = ""  />

         </ add >

         </ listeners >

    </ trace >

    </ system.diagnostics >

     

    上面的配置默认的模板已经帮助我们添加了。我们可以观察 Web.Config来确认。

    步骤四:运行程序

    F5运行程序。页面弹出。这时候代码应该已经执行了。过两分钟左右使用 Visual Studio中的 Server Explorer窗口,我们可以在 Development Storage WADLogsTable中看到一条记录:

     

    我们可以看到我们在代码中添加的跟踪信息已经被记录到 Table Storage中了。实际应用中我们可以通过 try-catch来捕获异常,并且添加错误日志。这些日志能够被传送到 cloud storage中以便分析问题。

    除了一般的日志以外, Windows Azure还提供了其他诊断方式,比如 Crash Dump Windows Event Log、性能计数器等等。

    有兴趣的读者可以参考 实现 Windows Azure诊断 进一步学习。

  • 相关阅读:
    Spring Boot 使用Redis
    openTSDB(转)
    httpClient 超时时间设置(转)
    HTTPClient 超时链接设置
    入坑python 自己写的小工具,纪念一下
    Linux下SVN创建新的项目
    java对象数组的概述和使用
    解决fastDFS客户端连接超时问题
    显示目录结构
    centos7开启80和8080端口
  • 原文地址:https://www.cnblogs.com/new0801/p/6176735.html
Copyright © 2020-2023  润新知