2017-05-09 83 views
0

我想將Azure雲診斷數據(特別是服務性能計數器)寫入機器實例的本地日誌文件,而不是將其保存在Azure存儲帳戶上。我怎麼做到這一點?將Azure雲服務性能計數器寫入本地文件

相關:根據此:Initialize or Change Azure Diagnostics Configuration默認情況下,將收集診斷數據並將其存儲在角色實例中。數據存儲在哪裏?這是我正在尋找的本地日誌文件嗎?

+0

你能告訴你爲什麼要將這些數據保存在本地文件中的原因嗎? –

回答

1

我嘗試獲取本地文件,即使我嘗試使用Powershell命令,但我也找不到性能計數器文件。

Get-ChildItem -Path disk:\ -recurse | Select-String -Pattern "search string" # search string in the file. 

根據我的經驗,將記錄存儲在存儲器中是一種很好的做法。然後我們可以使用SDK輕鬆查詢來自多個實例的記錄。

如果我們只是想獲取記錄到本地文件。我們可以使用Azure存儲SDK從WADPerformanceCountersTable獲取記錄,並將記錄保存到本地文件中作爲解決方法。

如果C#代碼可能適合您,請嘗試使用以下代碼,它是document的代碼片段。如何操作Azure存儲表請參考Get started with Azure Table storage

/ Get the connection string. When using Microsoft Azure Cloud Services, it is recommended 
// you store your connection string using the Microsoft Azure service configuration 
// system (*.csdef and *.cscfg files). You can you use the CloudConfigurationManager type 
// to retrieve your storage connection string. If you're not using Cloud Services, it's 
// recommended that you store the connection string in your web.config or app.config file. 
// Use the ConfigurationManager type to retrieve your storage connection string. 

string connectionString = Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("StorageConnectionString"); 
//string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString; 

// Get a reference to the storage account using the connection string. You can also use the development 
// storage account (Storage Emulator) for local debugging. 
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); 
//CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount; 

// Create the table client. 
CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); 

// Create the CloudTable object that represents the "WADPerformanceCountersTable" table. 
CloudTable table = tableClient.GetTableReference("WADPerformanceCountersTable"); 

// Create the table query, filter on a specific CounterName, DeploymentId and RoleInstance. 
TableQuery<PerformanceCountersEntity> query = new TableQuery<PerformanceCountersEntity>() 
    .Where(
    TableQuery.CombineFilters(
     TableQuery.GenerateFilterCondition("CounterName", QueryComparisons.Equal, @"\Processor(_Total)\% Processor Time"), 
     TableOperators.And, 
     TableQuery.CombineFilters(
     TableQuery.GenerateFilterCondition("DeploymentId", QueryComparisons.Equal, "ec26b7a1720447e1bcdeefc41c4892a3"), 
     TableOperators.And, 
     TableQuery.GenerateFilterCondition("RoleInstance", QueryComparisons.Equal, "WebRole1_IN_0") 
    ) 
) 
); 

// Execute the table query. 
IEnumerable<PerformanceCountersEntity> result = table.ExecuteQuery(query); 

// Process the query results and build a CSV file. 
StringBuilder sb = new StringBuilder("TimeStamp,EventTickCount,DeploymentId,Role,RoleInstance,CounterName,CounterValue\n"); 

foreach (PerformanceCountersEntity entity in result) 
{ 
    sb.Append(entity.Timestamp + "," + entity.EventTickCount + "," + entity.DeploymentId + "," 
    + entity.Role + "," + entity.RoleInstance + "," + entity.CounterName + "," + entity.CounterValue+"\n"); 
} 

StreamWriter sw = File.CreateText(@"C:\temp\PerfCounters.csv"); 
sw.Write(sb.ToString()); 
sw.Close(); 

更多信息。

+0

+1,這是有幫助的,如果我必須走這條路。僅供參考,我正在尋找性能計數器而不將它們發送到Azure存儲帳戶的方式,因爲我想將它們存儲在Azure環境之外的系統中。 – BobbyA

相關問題