Windows Azure入门教学系列 (九):Windows Azure 诊断功能
来源:互联网 发布:名侦探柯南 主线知乎 编辑:程序博客网 时间:2024/04/29 10:34
公告:本博客为微软云计算中文博客的镜像博客。部分文章因为博客兼容性问题,会影响阅读体验。如遇此情况,请访问原博客。
本文是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诊断进一步学习。
- Windows Azure入门教学系列 (九):Windows Azure 诊断功能
- Windows Azure入门教学系列
- Windows Azure入门教学系列 (八):使用Windows Azure Drive
- Windows Azure入门教学
- PHP on Windows Azure 入门教学系列(一):在Windows Azure内运行PHP应用
- Windows Azure入门教学系列 (四):使用Blob Storage
- Windows Azure入门教学系列 (五):使用Queue Storage
- Windows Azure入门教学系列 (六):使用Table Storage
- Windows Azure AppFabric 入门教学系列 (三):LABS环境
- Windows Azure入门教学系列 全面更新啦!
- Windows Azure(四-7)::诊断功能
- PHP on Windows Azure 入门教学系列(3) ——在Windows Azure中部署Wordpress
- PHP on Windows Azure 入门教学系列(2) ——利用SQL Azure做一个简单的访问计数器
- Windows Azure入门教学系列 (一): 创建第一个WebRole程序
- Windows Azure入门教学系列 (二): 部署第一个Web Role程序
- Windows Azure入门教学系列 (三):创建第一个Worker Role程序
- Windows Azure入门教学系列 (七):使用REST API访问Storage Service
- Windows Azure AppFabric 入门教学系列 (一):创建 AppFabric 项目与命名空间
- make,make clean,make depend的区别
- HTTP 状态代码 GOOGLE
- 解决CLUSTER不能关闭的问题!
- 用隐含参数强制打开了一个损坏的数据库!
- html-css-js的几款前端开发工具
- Windows Azure入门教学系列 (九):Windows Azure 诊断功能
- 老业务员的忠告【销售为王】
- 在ASP.NET操作Excel文件
- 在窗体上嵌入 Windows Media Player
- Struts2 整合Fckeditor 2.6 简例
- 黑客宣言(1986年)The Hacker Manifesto (1986)
- 站长统计
- 后台开发也要快速原型
- Ez4w的VPS内存升级了