2017-05-09 125 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

相关问题