在ASP.NET Core应用中获取Azure Insights遥测数据/搜索日志

时间:2020-05-14 16:57:48

标签: c# azure asp.net-core azure-application-insights

在C#应用程序中是否有一种方法使用Azure Nuget包搜索Azure Telemetry数据并获取某种json结果集?

我有一个ASP.NET Core 2.x管理应用程序,我想查看最近的X Azure成功的Azure webjob功能(请参见屏幕截图)。我似乎找不到有关如何在应用程序中运行此查询的任何信息。

azurescreenshot

1 个答案:

答案 0 :(得分:2)

我不知道有一个封装了此内容的Nuget程序包,但是App Insights公开了一个REST API,并且它们具有一个API Quickstart guide

以下是C#示例:

private const string URL =
        "https://api.applicationinsights.io/v1/apps/{0}/{1}/{2}?{3}";

public static string GetTelemetry(string appid, string apikey,
        string queryType, string queryPath, string parameterString)
{
    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Add("x-api-key", apikey);
    var req = string.Format(URL, appid, queryType, queryPath, parameterString);
    HttpResponseMessage response = client.GetAsync(req).Result;
    if (response.IsSuccessStatusCode)
    {
        return response.Content.ReadAsStringAsync().Result;
    }
    else
    {
        return response.ReasonPhrase;
    }
}

...

var json = GetTelemetry
    ("DEMO_APP",  "DEMO_KEY",
        "metrics", "requests/duration", "interval=PT1H");

底部的示例呼叫将返回过去24小时每小时总计的请求持续时间(示例路线的默认时间范围)。

可以通过“ API访问”菜单的每个特定Application Insights实例的“配置”标题下创建API密钥: enter image description here