anon
anon

Reputation:

How to create email alerts based on inside code functionality of Azure Function App - Timer Trigger?

Scenario: Enabling Health Check Monitoring of APIM APIs and Email Alerts if the API is not healthy.

Using: Azure Functions - C# - Consumption Based Model

using (var httpClient = new HttpClient())

            {

                httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
                try
                {
                    var response = await httpClient.GetAsync(url);
                    var statusCode = response.StatusCode;
                    if (statusCode == HttpStatusCode.OK)
                    {
                        log.LogInformation($"URL is healthy. Status code: {statusCode}");
                    }
                    else
                    {
                        log.LogError($"URL is not healthy. Status code: {statusCode}");
                        // Trigger an alert or take any other action here using Azure Monitor or other monitoring solutions.
                    }
                }
                catch (Exception ex)
                {
                    log.LogError($"Error occurred while checking the URL: {ex.Message}");
                   // Handle any exceptions that might occur during the request.
                }
            }

Result:

URL is healthy. Status code: OK - This is the log message written inside the Azure Function Code.

I Know how to create alerts if azure function fails to run but I'm not getting idea how to create alerts (Eg: email alerts) when the above function gives the result as URL is not healthy. Status code: {statusCode}.

Upvotes: 0

Views: 170

Answers (0)

Related Questions