[VS2010] Visual Studio 2010 與 Windows Azure: 將診斷與追蹤資訊傳遞給 Windows Azure Storage 儲存體

[VS2010] Visual Studio 2010 與 Windows Azure: 將診斷與追踪資訊傳遞給 Windows Azure Storage 儲存體

在開發階段,我們大多都是使用 Development Storage 以及 Development Fabric 來測試應用程式,而寫在 Web Role 以及 Worker Role 中的每個 Trace 輸出,都會顯示在 Development Fabric 訊息窗中:

 

image

 

但是程式一旦部署到雲端,這個視窗就沒用了,因為 development fabric 看不到雲端的診斷資訊,而且 Trace 的訊息也不知道丟到哪去了,因此必須要把 Trace 資訊轉向到一個儲存的地方保存,事後才可以撰寫額外的程式來取得這些 Trace 的資訊,以利後續的監控與除錯。

 

在 Windows Azure 中,應用程式可以利用 Trace 類別來輸出訊息,如果對 .NET Framework 熟悉的人,應該也知道 Trace 會跟著一個 TraceListener,而且 .NET Framework 中也有好幾個內建的 TraceListener 的實作品,在 Windows Azure 中當然也有,位在 Microsoft.WindowsAzure.Diagnostics.dll 中,有一個 DiagnosticMonitorTraceListener 類別,即為負責收集來自應用程式的 Trace 訊息,不過 DiagnosticMonitorTraceListener 不只是收集 Trace 資訊而已,還有執行在 Windows Azure 應用程式虛擬機器中的一切事物,有:

 

  • Windows Azure Logs: 由應用程式的 Trace 指令輸出的記錄訊息。
  • IIS 7.0 Logs: 來自執行個體 VM 中的 IIS 的記錄檔。
  • Windows Diagnostics Infrastructure Logs: 來自於 Windows Azure Fabric Controller 的訊息。
  • Failed Request Logs: 將 IIS 的 Failed Request 啟用後捕捉到的訊息。
  • Windows Event Logs: 來自執行個體 VM 的 Windows 事件記錄。
  • Performance Counter Logs: 來自執行個體 VM 的效能計數器的記錄數據。
  • Crash Dumps: 來自執行個體 VM 中的當機記錄。
  • Custom Error Logs: 自訂的錯誤記錄。

 

這些記錄檔設定都可以由 DiagnosticMonitorConfiguration 來加以設定,像是儲存間隔,儲存位置(若記錄會儲存到 BLOB 時),記錄類型等等,例如下列的程式碼是設定抓取 Windows 事件記錄中的系統記錄,並且每分鐘將它轉存到儲存體中:

 

DiagnosticMonitorConfiguration diagConfig = DiagnosticMonitor.GetDefaultInitialConfiguration();
diagConfig.WindowsEventLog.DataSources.Add("Application!*");
diagConfig.WindowsEventLog.ScheduledTransferPeriod = System.TimeSpan.FromMinutes(1.0);
DiagnosticMonitor.Start("DiagnosticsConnectionString", diagConfig);

 

雖然每個事件記錄都可以存到儲存體中,但是不是每個記錄都會儲存到 Table,會依據不同的事件類型有不同的儲存體要求:

 

  • Windows Azure Logs: Table Storage。
  • IIS 7.0 Logs: BLOB Storage。
  • Windows Diagnostics Infrastructure Logs: Table Storage。
  • Failed Request Logs: BLOB Storage。
  • Windows Event Logs: Table Storage。
  • Performance Counter Logs: Table Storage。
  • Crash Dumps: BLOB Storage。
  • Custom Error Logs: BLOB Storage。

     

    例如,下列圖示即顯示了 Windows Azure Logs (WADLogsTable) 以及 Diagnostics Infrastructure Logs (WADDiagnosticInfrastructureLogsTable) 的記錄儲存表格,這兩個表格只有在程式中指示要轉存記錄到 Table Storage 時才會出現:

     

    image

     

    例如下列程式會啟用 Windows Azure Logs 以及 Diagnostic Infrastructure Logs 的記錄:

     

    DiagnosticMonitorConfiguration diagConfiguration = DiagnosticMonitor.GetDefaultInitialConfiguration();
    diagConfiguration.Logs.ScheduledTransferPeriod = new TimeSpan(0, 10, 0);
    diagConfiguration.DiagnosticInfrastructureLogs.ScheduledTransferPeriod = new TimeSpan(0, 10, 0);
    diagConfiguration.WindowsEventLog.ScheduledTransferPeriod = new TimeSpan(0, 10, 0);
    diagConfiguration.Directories.DataSources.Add(new DirectoryConfiguration() { Container = "myfile", Path = "log.txt" });

    DiagnosticMonitor.Start("DiagnosticsConnectionString", diagConfiguration);

     

    另外,若要啟用記錄傳遞,除了要設定 DiagnosticMonitorConfiguration 外,還要注意其連線字串必須要與應用程式使用的不同,因此可利用儲存服務的第二個連接金鑰 (secondary key) 來處理儲存記錄的功能。

     

    參考資料:

    Implement Windows Azure Diagnostics