![](/img/trans.png)
[英]Fetching HTML content from Angular4 page with HttpClient from ASP.NET web application
[英]Fetching data from Application insights (Azure Portal) to display it on a web page in asp.net web application
我已經使用應用程序洞察為我的 .net Web 應用程序配置了 azure 門戶。 現在我想要的是門戶中顯示的詳細信息應該顯示在我的 .net Web 應用程序的網頁上。 我不知道怎么做,並希望有人在這方面幫助我。 我還分享了我想要從我的 azure 門戶中獲取的實際內容的快照,以在我的應用程序網頁上顯示為網格報告。 [
有一個用於從 Application Insights 檢索數據的 API。 從文檔中,您可以“查詢並集成 Application Insights 為您的應用程序收集的性能、可用性和使用數據”和“使用強大而簡單的 REST API 訪問您應用程序的所有事件和指標數據”。
一些示例請求:
返回最后一天的請求總數(timespan=P1D):
GET /v1/apps/DEMO_APP/metrics/requests/count?timespan=P1D HTTP/1.1
過去 6 小時的每小時平均服務器響應時間(間隔=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 的方法:
編輯2:
好的,一旦您創建了 API 密鑰,您就可以將它與您的應用程序 ID 一起使用,以從 Application Insights 返回數據。 您可以使用具有以下格式的公共 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 密鑰。 以下是獲取過去 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.