如何为从未触发的警报创建 azure customMetric 警报?

How can I create an azure customMetric alert for an alert that has never fired?

我在产品环境中有一些错误警报,我希望在它们触发时收到警报。问题是他们从来没有解雇过,所以我在门户网站上看不到他们。

谢谢!

我看到你有一个try/catch,那么你可以考虑发送一个自定义的错误消息给application insights(这里的逻辑是,既然你知道自定义的错误消息,你可以使用这个pre-defined 消息来创建警报),方法是在您的 catch 块中使用以下代码:

        try
        {
           //your code            
        }
        catch
        {
            //if you're using ILogger which is integrated with application insights.
            _logger.LogError(new Exception(),"custom error: xxxx");
                           
            //your other code
        }

然后导航到 Azure 门户 -> 应用程序见解 -> 日志:

1.Write 如下查询:

exceptions  
| where customDimensions.FormattedMessage == "custom error: xxxx"

2.Select查询,然后点击运行按钮。

3.At 最后,单击“新建警报规则”按钮创建警报。

我想我现在更了解你的问题了。今天我收到使用 this post 的新功能通知:

Define an alert rule on a custom metric that isn't emitted yet

When creating a metric alert rule, the metric name is validated against the Metric Definitions API to make sure it exists. In some cases, you'd like to create an alert rule on a custom metric even before it’s emitted. For example, when creating (using an Resource Manager template) an Application Insights resource that will emit a custom metric, along with an alert rule that monitors that metric.

To avoid having the deployment fail when trying to validate the custom metric’s definitions, you can use the skipMetricValidation parameter in the criteria section of the alert rule, which will cause the metric validation to be skipped. (source)