将Azure云服务性能计数器编写到本地文件

时间:2017-05-09 21:36:43

标签: azure azure-cloud-services azure-diagnostics

我想将Azure Cloud诊断数据(特别是服务性能计数器)写入计算机实例上的本地日志文件,而不是将其保存在Azure存储帐户上。我该如何做到这一点?

相关:根据:Initialize or Change Azure Diagnostics Configuration“默认情况下,”将收集诊断数据并将其存储在角色实例中“。存储的数据在哪里?这是我正在寻找的本地日志文件吗?

1 个答案:

答案 0 :(得分:1)

我试图获取本地文件,即使我尝试使用Powershell命令,但我也找不到性能计数器文件。

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

根据我的经验,将记录存储在存储中是一种很好的做法。然后我们可以使用SDK轻松查询来自多个实例的记录。

如果我们只想将记录记录到本地文件中。我们可以使用Azure存储SDK从 WADPerformanceCountersTable 获取记录,并将记录保存到本地文件中作为解决方法。

如果您可以使用C#代码,请尝试使用以下代码,该代码是document的代码段。

/ 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();

有关如何操作Azure存储表的更多信息,请参阅Get started with Azure Table storage