如何在Visual Studio MVC项目中使用application Insights REST API

问题描述 投票:0回答:1

我想检索azure门户网站上我资源中存在的所有数据。我发现有一个REST API可用于帮助检索数据的应用程序洞察。我想要的是获取数据并在我的网页上生成网格报告,该报告显示事件相关信息,即日期,类型,消息和所有相关信息。我以前没有使用REST API,我想要的帮助是在Visual Studio中基于MVC的web项目中使用此REST API的正确指南。如果有人可以帮助将是一个很大的帮助。

c# asp.net-mvc model-view-controller visual-studio-2015 azure-application-insights
1个回答
2
投票

您可以按照以下步骤操作:

第1步:获取应用程序ID和API密钥。

导航到您的应用程序见解 - > API Access,请参阅屏幕截图(请记住,生成api密钥时,请将其写下来):enter image description here

第2步:了解API格式,有关详细信息,请参阅here

以下是过去6小时内获取请求数的示例:

https://api.applicationinsights.io/v1/apps/your-application-id/metrics/requests/count?timespan=PT6H

这部分https://api.applicationinsights.io/v1/apps/不需要改变。

然后输入你从上一步得到的your-application-id

然后您可以根据需要指定metricsevents

这部分requests/count,你可以参考this,截图如下:enter image description here

最后一部分?timespan=PT6H,你可以参考this,截图如下:enter image description here

第3步:编写代码来调用此api,如下所示:

public class Test
{
 private const string URL_requests = "https://api.applicationinsights.io/v1/apps/your-application-id/metrics/requests/count?timespan=PT6H";

 public string GetRequestsCount()
        {
            // in step 1, you get this api key
            string apikey = "flk2bqn1ydur57p7pa74yc3aazhbzf52xbyxthef";

            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("x-api-key", apikey);
            var req = string.Format(URL_requests);
            HttpResponseMessage response = client.GetAsync(req).Result;
            if (response.IsSuccessStatusCode)
            {
                // you can get the request count here
                return response.Content.ReadAsStringAsync().Result;
            }
            else
            {
                return response.ReasonPhrase;
            }
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.