c# - 从 Application insights (Azure Portal) 获取数据以将其显示在 asp.net web 应用程序的网页上

标签 c# asp.net-mvc visual-studio-2015 azure-application-insights

我已使用应用程序洞察为我的 .net Web 应用程序配置了 Azure 门户。现在我想要的是门户中显示的详细信息应该显示在我的 .net 网络应用程序的网页上。我不知道该怎么做,希望有人在这方面帮助我。我还分享了我实际上想从我的 Azure 门户中获取的内容的快照,以在我的应用程序网页上显示为网格报告。 [ I want these two marked parts as a part of my web page, that is, one as a division and the other as a grid report.[1]

最佳答案

有一个 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 的方法:

  1. 点击您的应用服务,然后在概览面板中您应该会看到 Application Insights 的链接:

App Service Overview

  1. 添加此项后再次点击应用服务并在设置部分选择 Application Insights,然后滚动并选择 API 访问:

API Access

  1. 点击创建 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/

相关文章:

visual-studio-2015 - Microsoft Bot Framework,Visual Studio 2015和2017出现启动错误

C# Selenium WebDriver 测试在连续运行时失败但在单独运行时成功

c# - 如何在虚拟现实 (GearVR) 应用程序中打开 URL

jquery - ASP.NET MVC2 和 JQuery 的高级最佳实践

html - MVC : Not render css class for certain view?

c# - Entity Framework Core 中的自引用多对多关系

c# - asp.net mvc 5 DropDownList nullable int default option not null

c# - 获取摩尔多瓦的区域信息

asp.net-mvc - 如何测试 ASP.Net MVC View ?

c# - 使用 C# 连接到 Visual Studio 2015 中的 Team Foundation Server?