简体   繁体   English

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

[英]How to Use application Insights REST API in Visual Studio MVC project

I want to retrieve all the data that is present in my resource on the azure portal.我想检索 Azure 门户上我的资源中存在的所有数据。 I have found out that there is a REST API for application insights that can help in retrieving the data.我发现有一个用于应用程序洞察的 REST API 可以帮助检索数据。 What I want is to get the data and generate a grid report on my web page which displays the events related information, that is, date, type, message and all the related information.我想要的是获取数据并在我的网页上生成一个网格报告,该报告显示事件相关信息,即日期、类型、消息和所有相关信息。 I haven't worked with REST API's before and what I want as a help is a proper guideline to use this REST API in my MVC based web project in visual studio.我之前没有使用过 REST API,我想要的帮助是在 Visual Studio 的基于 MVC 的 Web 项目中使用此 REST API 的正确指南。 If anyone can help will be a great assistance.如果有人可以帮助将是一个很大的帮助。

You can follow the steps below:您可以按照以下步骤操作:

step 1: Get the Application ID and an API key.步骤 1:获取应用程序 ID 和 API 密钥。

Nav to your application insights -> API Access, see the screenshot(Please remember, when the api key is generated, write it down):导航到你的应用洞察-> API访问,看截图(请记住,api key生成的时候,写下来): 在此处输入图片说明

step 2: Understand the API Format, for details, refer to here :第二步:了解API Format,具体参考这里

Here is an example for get requests count in the last 6 hours:以下是过去 6 小时内获取请求计数的示例:

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

This part https://api.applicationinsights.io/v1/apps/ do not need to change.这部分https://api.applicationinsights.io/v1/apps/不需要更改。

Then input your-application-id which you get from last step.然后输入your-application-id从上一步获得的your-application-id

Then you can specify metrics or events as per your demand.然后,您可以根据需要指定metricsevents

This part requests/count , you can refer to this , screenshot below:这部分requests/count ,你可以参考这个,截图如下: 在此处输入图片说明

The last part ?timespan=PT6H , you can refer to this , screenshot below:最后一部分?timespan=PT6H ,你可以参考这个,截图如下: 在此处输入图片说明

step 3: Write your code to call this api, like below:第 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;
            }
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止 Visual Studio 显示应用程序洞察提醒? - How to I prevent Visual Studio from showing application insights reminder? 如何通过REST API创建Azure Application Insights的新实例 - How to create a new instance of Azure Application Insights through the REST API Visual Studio中类库的Application Insights - Application Insights for class library in visual studio Visual Studio Application Insights工具栏不显示事件 - Visual Studio Application Insights toolbar not showing events 如何在Visual Studio中组织.NET MVC应用程序 - How to organize a .NET MVC application in Visual Studio Visual Studio 2010 ///.NET MVC应用程序-空白项目出现404错误 - Visual Studio 2010 //.NET MVC Application - 404 Error on blank project 如何从 ASP.NET Core 应用程序将应用程序洞察数据发布到 REST API 而不是 Azure? - How to post application insights data to REST API instead of Azure from ASP.NET Core application? 如何在Visual Studio 2013中为MVC项目添加测试项目 - How to Add Test Project for MVC Project in Visual Studio 2013 API Application Insights使用的良好实践 - API Application Insights good practice to use 如何在 C#、Visual Studio 2022 中的 NUnit 项目中运行 web 应用程序项目 (ASP.NET MVC) - How to run a web application project (ASP.NET MVC) within C#, NUnit project in Visual Studio 2022
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM