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

标签 c# asp.net-mvc azure visual-studio-2015 azure-application-insights

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

最佳答案

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

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

导航到您的应用程序洞察 -> API 访问,请参阅屏幕截图(请记住,当生成 api key 时,请将其记下来): enter image description here

第二步:了解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

然后您可以根据您的需求指定指标事件

这部分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;
            }
        }
}

关于c# - 如何在 Visual Studio MVC 项目中使用 application Insights REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51785950/

相关文章:

c# - 如何在 .Net Core 2.0 中的 WebAPI 上使用 Azure 中的应用程序设置

c# - ASP.NET MVC 和实体 - 使用原始 SQL 时加载导航属性/对象

Azure 媒体服务 : storage encrypted rest api didn't work

azure - DNN 9.01.01 导出/导入卡在已提交状态

c# - 将 Identity 2.0 抽象到领域模型层

c# - 如何获取 ItemDataBound 中 Repeater 中的项目计数

asp.net - 将大文件上传到 ASP.NET MVC

php - 通过 azure graph api 更新时权限不足,无法完成操作

c# - 定义动态添加发件人的事件处理程序

javascript - 将模型变量放入js代码中