【问题标题】:Asp.net Core - Read Application Insights Page ViewsAsp.net Core - 阅读 Application Insights 页面视图
【发布时间】:2021-02-22 21:33:24
【问题描述】:
一个很简单的问题,我本来想,但一直找不到答案。
我在我的 Asp.net core 3.1 mvc 应用程序上启用了 Application Insights。我已经能够在 azure 门户中查看数据,并看到应用程序具有页面浏览量。
我现在想做的是在我的 mvc Web 应用程序中检索页面视图以进行报告。
有没有办法做到这一点?
【问题讨论】:
标签:
asp.net-core
azure-application-insights
【解决方案1】:
我现在想做的是在我的 mvc Web 应用程序中检索页面视图以进行报告。
metrics REST API 使我们能够根据指定的metricId 获取指标值。
GET https://api.applicationinsights.io/v1/apps/{appId}/metrics/{metricId}
要获取有关页面浏览量的指标,您可以参考以下代码 sn-p to make request from your ASP.NET Core MVC app。
var client = _clientFactory.CreateClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("x-api-key", apikey);
HttpResponseMessage response = client.GetAsync($"https://api.applicationinsights.io/v1/apps/{appid}/metrics/pageViews/count?timespan=2021-02-19%2F2021-02-24").Result;
if (response.IsSuccessStatusCode)
{
var val = System.Text.Json.JsonSerializer.Deserialize<PvData>(response.Content.ReadAsStringAsync().Result);
var sum = val.value.PageViewsCount.sum;
}
模型类
public class PageViewsCount
{
public int sum { get; set; }
}
public class Value
{
public DateTime start { get; set; }
public DateTime end { get; set; }
[JsonPropertyName("pageViews/count")]
public PageViewsCount PageViewsCount { get; set; }
}
public class PvData
{
public Value value { get; set; }
}
测试结果