我已使用应用程序洞察为我的 .net Web 应用程序配置了 Azure 门户。现在我想要的是门户中显示的详细信息应该显示在我的 .net 网络应用程序的网页上。我不知道该怎么做,希望有人在这方面帮助我。我还分享了我实际上想从我的 Azure 门户中获取的内容的快照,以在我的应用程序网页上显示为网格报告。 [
最佳答案
有一个 API 可用于从 Application Insights 检索数据。从文档中,您可以“查询并集成 Application Insights 为您的应用程序收集的性能、可用性和使用情况数据”以及“使用功能强大且简单的 REST API 访问您应用程序的所有事件和指标数据”。
一些示例请求:
返回最近一天的请求总数(timespan=P1D):
GET /v1/apps/DEMO_APP/metrics/requests/count?timespan=P1D HTTP/1.1
过去 6 小时的平均每小时服务器响应时间 (interval=PT1H):
GET /v1/apps/{app-id}/metrics/requests/duration?timespan=PT6H&interval=PT1H
列出最近 5 个事件:
GET /v1/apps/{app-id}/events/$all?$top=5
列出失败或耗时超过 0.5 秒的 GET 请求:
GET /v1/apps/{app-id}/events/requests?$filter=startswith(request/name, 'GET') and (request/resultCode ne '200' or request/duration gt 500)
更多信息在这里:
https://dev.applicationinsights.io/
编辑:
以下是添加 Application Insights 的方法:
- 点击您的应用服务,然后在概览面板中您应该会看到 Application Insights 的链接:
- 添加此项后再次点击应用服务并在设置部分选择 Application Insights,然后滚动并选择 API 访问:
- 点击创建 API key
编辑 2:
好的,一旦您创建了 API key ,您就可以使用它和您的应用程序 ID 从 Application Insights 返回数据。您可以使用具有以下格式的公共(public) API 执行此操作:
https://api.applicationinsights.io/{version}/apps/{app-id}/{operation}/[path]?[parameters] X-API-Key:{key}
您可以使用此页面查询数据,也可以使用类似 Postman 或 cUrl 的工具:
https://dev.applicationinsights.io/apiexplorer/metrics
您需要提供可以在 Azure 门户中找到的应用程序 ID 以及您生成的 API key 。以下是获取过去 30 天的请求总数的 GET 请求示例:
GET /v1/apps/{yourApplicationId}/metrics/requests/count?timespan=P30D HTTP/1.1
Host: api.applicationinsights.io
x-api-key: {yourAPIKey}
当您的 API 调用正常工作时,您可以使用任何您想要检索数据的客户端,例如Angular、jQuery、C# HttpClient 等
编辑 3:
好的,这是一个基本但完整的 html 页面,用于从您的应用程序中检索应用洞察数据。您需要做的就是用您的值替换“{applicationId}”和“{api-key}”。您可以在 Azure 门户中获得这些内容 - 单击前面屏幕截图中所示的“Application Insights”和“API Access”部分。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>App Insights Sample</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
function getAppInsightsData() {
const userAction = async () => {
const url = 'https://api.applicationinsights.io/v1/apps/{applicationId}/metrics/requests/count?timespan=P30D';
const myHeaders = { headers: new Headers({
'x-api-key': '{api-key}'
})
}
const response = await fetch(url, myHeaders);
const myJson = await response.json();
document.getElementById('p1').innerHTML = 'Date Range: ' + myJson.value.start + ' to ' + myJson.value.end + '. Requests: ' + myJson.value["requests/count"].sum;
}
userAction();
}
</script>
</head>
<body>
<button type="submit" onclick="javascript:getAppInsightsData()">Get data using fetch</button>
<div id='div'>
<p id='p1'></p>
</div>
</body>
</html>
关于c# - 从 Application insights (Azure Portal) 获取数据以将其显示在 asp.net web 应用程序的网页上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51761528/