我对 Azure 还很陌生。以下是我想要实现的任务:
我想使用 C# 代码从 .Net 应用程序使用 Azure Monitor Rest API,并想在网页上显示一些指标(任意几个指标)(我正在使用 ASP.Net)。
为此,我创建了 Azure AD,从 Azure 门户获取了订阅 ID、租户 ID、客户端密钥和客户端 ID。我所要做的就是从.Net端开始,这是平衡性的,我找不到合适的资源来完成这个任务。
有人可以帮我吗?
Microsoft.Azure.Management.Monitor
软件包现已弃用,建议使用 Azure.ResourceManager.Monitor
软件包替代方案。
我无法从快速谷歌中找到任何示例代码,因此在这里添加一些快速而肮脏的示例。
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Monitor;
using Azure.ResourceManager.Monitor.Models;
ArmClient client = new ArmClient(new DefaultAzureCredential());
ResourceIdentifier scope = new ("REDACTED RESOURCEID");
ArmResourceGetMonitorMetricsOptions options = new ArmResourceGetMonitorMetricsOptions()
{
Aggregation = "total",
Filter = "CapacityType eq '*' and DatabaseName eq '*' and Region eq '*' and CollectionName eq '*'",
Orderby = "total desc",
Top = 10000,
Metricnames = "TotalRequestUnits",
Timespan = "2024-04-26T16:00:00.000Z/2024-04-27T17:00:00.000Z",
Interval = TimeSpan.FromHours(1)
};
await foreach (var metric in client.GetMonitorMetricsAsync(scope, options))
{
foreach (var monitorTimeSeriesElement in metric.Timeseries)
{
//Not sure if I missed any inbuilt method to retrieve the metadata values more elegantly!
var capacityType = monitorTimeSeriesElement.Metadatavalues.Single(md => md.Name.Value == "CapacityType".ToLowerInvariant()).Value;
var databaseName = monitorTimeSeriesElement.Metadatavalues.Single(md => md.Name.Value == "DatabaseName".ToLowerInvariant()).Value;
var collectionName = monitorTimeSeriesElement.Metadatavalues.Single(md => md.Name.Value == "CollectionName".ToLowerInvariant()).Value;
var region = monitorTimeSeriesElement.Metadatavalues.Single(md => md.Name.Value == "Region".ToLowerInvariant()).Value;
Console.WriteLine(databaseName + " -> " + collectionName + " -> " + region + " -> " + capacityType);
foreach (var metricValue in monitorTimeSeriesElement.Data)
{
Console.WriteLine(metricValue.TimeStamp + " -> " + metricValue.Total);
}
}
}