azure警报解析库c#



我被分配了一项任务来解析azure警报并从azure警报中提取相关信息。是否存在解析azure警报并提取资源组/订阅/资源名称和警报详细信息的nuget包?以下是示例警报格式

{"schemaId"azureMonitorCommonAlertSchema"data" {"essentials" {"alertId"b6a1c5/订阅/63 - 3552 - 4676 - 8073 - c31e2641f787/供应商/Microsoft.AlertsManagement/警告/7 cf70adc c221 - 467 c - bcfe bab9e33d98b2","alertRule"AlertMetricCPUPercentage"severity"Sev0"signalType"Metric"monitorCondition"Fired"monitoringService"Platform"alertTargetIDs"("b6a1c5/订阅/63 - 3552 - 4676 - 8073 c31e2641f787/resourcegroups/桥/供应商/microsoft.compute/virtualmachines/vm01"],"configurationItems"("vm01"],"originAlertId"63 b6a1c5 - 3552 - 4676 - 8073 - c31e2641f787_microsoft.insights_metricalerts_alertmetriccpupercentage_ - 1419778843","firedDateTime"2021 - 05 - 21 - t18:20:57.1495223z"description";如果CPU %小于10%","essentialsVersion"1.0","alertContextVersion"1.0";},"alertContext" {"properties"空,"conditionType"MultipleResourceMultipleMetricCriteria"condition" {"windowSize"PT5M"allOf"({metricName": "Percentage CPU"metricNamespace"Microsoft.Compute/virtualMachines"operator"LessThan"threshold"10","timeAggregation"Maximum"dimensions" [],"metricValue" 0.95,"webTestName"零}],"windowStartTime"2021 - 05 - 21 - t18:12:44.375z"windowEndTime"2021 - 05 - 21 - t18:17:44.375z"}}}}

如上所述,它只是JSON数据,所以您只需创建一个c#类映射到您感兴趣的属性,然后将数据反序列化为该类的对象。

这是微软文档中关于JSON数据反序列化的一段代码

using System;
using System.Collections.Generic;
using System.Text.Json;
namespace DeserializeExtra
{
public class WeatherForecast
{
public DateTimeOffset Date { get; set; }
public int TemperatureCelsius { get; set; }
public string Summary { get; set; }
public string SummaryField;
public IList<DateTimeOffset> DatesAvailable { get; set; }
public Dictionary<string, HighLowTemps> TemperatureRanges { get; set; }
public string[] SummaryWords { get; set; }
}
public class HighLowTemps
{
public int High { get; set; }
public int Low { get; set; }
}
public class Program
{
public static void Main()
{
string jsonString =
@"{
""Date"": ""2019-08-01T00:00:00-07:00"",
""TemperatureCelsius"": 25,
""Summary"": ""Hot"",
""DatesAvailable"": [
""2019-08-01T00:00:00-07:00"",
""2019-08-02T00:00:00-07:00""
],
""TemperatureRanges"": {
""Cold"": {
""High"": 20,
""Low"": -10
},
""Hot"": {
""High"": 60,
""Low"": 20
}
},
""SummaryWords"": [
""Cool"",
""Windy"",
""Humid""
]
}
";

WeatherForecast weatherForecast = 
JsonSerializer.Deserialize<WeatherForecast>(jsonString);
Console.WriteLine($"Date: {weatherForecast.Date}");
Console.WriteLine($"TemperatureCelsius: {weatherForecast.TemperatureCelsius}");
Console.WriteLine($"Summary: {weatherForecast.Summary}");
}
}
}
// output:
//Date: 8/1/2019 12:00:00 AM -07:00
//TemperatureCelsius: 25
//Summary: Hot

这是实际的文章https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0

最新更新