Azure Lei Zhang的博客

weibo: LeiZhang的微博/QQ: 185165016/QQ群:319036205/邮箱:leizhang1984@outlook.com/TeL:139-161-22926

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

  《Windows Azure Platform 系列文章目录

 

  如果我们在本地服务器上调试应用程序,我们可以使用Visual Studio设置断点或者输出日志文件的方式。

  但是我们把应用程序部署到云端服务器上之后,就不能使用传统的方式来调试应用程序了。那我们部署到云端的应用程序运行时如何监视并且发现和修改Bug呢?

  Windows Azure提供了诊断功能,该功能能够记录诊断日志,保存到Windows Azure Storage里。

1.我们先创建一个Cloud Project并且添加一个Web Role

2.在Default.aspx里添加一个如下内容:

<asp:TextBox ID="txbInput" runat="server"></asp:TextBox>

<asp:Button ID="btnOK" runat="server" Text="确认" onclick="btnOK_Click" />

3.在Default.aspx.cs添加如下代码:

4.在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();

}
 protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine(System.DateTime.Now.ToLongTimeString() + "Page_Load is called");
}

protected void btnOK_Click(object sender, EventArgs e)
{
string inputValue = txbInput.Text.Trim();

if (!string.IsNullOrEmpty(inputValue))
{
System.Diagnostics.Trace.WriteLine(System.DateTime.Now.ToLongTimeString() + "Your Input is " + inputValue);
}
}


5.分析代码:

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

diagConfig.Logs.ScheduledTransferLogLevelFilter =LogLevel.Verbose;

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

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

这段代码设置了传输间隔。这里我们设置为每隔一分钟传输一次日志

6.修改配置文件

Cloud Project-->Roles-->右键-->属性

 

修改Enable Diagnostics,输入Azure Storage Account Name和Account Key

把Azure诊断内容保存到Windows Azure Storage里。

7.最后把Web站点发布到Windows Azure托管服务上(过程略),并且登陆该站点。

8.使用Visual Studio中的Server Explorer,右键-->New Account, Add Account Name和Account Key,我们可以在threestone帐号下看到WADLogsTable看到一条记录:

 

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

 

大家有兴趣可以参考微软MSDN:使用Windows Azure诊断收集日志记录数据

Using IntelliTrace to debug Windows Azure Cloud Services


 

posted on 2012-01-30 15:48  Lei Zhang的博客  阅读(2382)  评论(0编辑  收藏  举报