[英]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.然后,您可以根据需要指定
metrics
或events
。
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.