You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#解析Azure告警提取信息 可用的Nuget包有哪些?

解决方案

首先明确:微软没有发布专门仅用于Azure告警解析的独立Nuget包,但有两种稳定的实现方案可以直接完成字段提取需求:

方案1:使用官方Azure SDK强类型解析(推荐)

  • 安装Nuget包:Azure.ResourceManager.AlertsManagement
  • 包内已经内置了Azure Monitor通用告警Schema的所有实体类,无需手动定义序列化结构,直接将告警JSON反序列化为对应类型即可读取所有字段,包括告警规则、级别、触发时间、资源ID、指标阈值条件等所有需要的信息。
  • 额外优势:SDK内置了ARM资源ID解析方法,不需要手动拆分字符串即可直接提取订阅ID、资源组、资源名称等属性,适配多资源告警、不同类型资源告警的场景。

方案2:轻量自定义解析(无额外依赖)

如果不想引入完整Azure SDK,直接用.NET自带的System.Text.Json或者常用的Newtonsoft.Json做自定义解析即可,参考示例代码如下:

using System.Text.Json;

// 入参alertJson为收到的Azure告警原始JSON字符串
JsonElement alertRoot = JsonDocument.Parse(alertJson).RootElement;
JsonElement essentialsNode = alertRoot.GetProperty("data").GetProperty("essentials");

// 提取基础告警信息
string alertRuleName = essentialsNode.GetProperty("alertRule").GetString();
string alertSeverity = essentialsNode.GetProperty("severity").GetString();
string triggerTime = essentialsNode.GetProperty("firedDateTime").GetString();
string resourceArmId = essentialsNode.GetProperty("alertTargetIDs")[0].GetString();

// 拆分ARM ID提取资源属性
string[] armIdSegments = resourceArmId.Split('/', StringSplitOptions.RemoveEmptyEntries);
string subscriptionId = armIdSegments[1];
string resourceGroupName = armIdSegments[3];
string resourceName = armIdSegments[^1];

// 提取告警触发明细
JsonElement conditionNode = alertRoot.GetProperty("data").GetProperty("alertContext").GetProperty("condition");
JsonElement firstCriteria = conditionNode.GetProperty("allOf")[0];
string metricName = firstCriteria.GetProperty("metricName").GetString();
double actualValue = firstCriteria.GetProperty("metricValue").GetDouble();
string threshold = firstCriteria.GetProperty("threshold").GetString();

内容的提问来源于stack exchange,提问作者dumtechie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 14:09:04