在 C#.Net 中的 Azure 函数中查询 Azure Application Insights CustomEvents

问题描述 投票:0回答:1

我需要在天蓝色函数中的应用程序洞察下查询 CustomEvents。

我能够使用以下包读取 CustomEvents: Microsoft.Azure.ApplicationInsights.Query

这是代码:

string applicationId = "xxxx-xxxx-xxxx";
string key = "xxxxxxxxxxx";

// Create client
var credentials = new ApiKeyClientCredentials(key);
var applicationInsightsClient = new ApplicationInsightsDataClient(credentials);

// Query Application Insights
var query = "customEvents" +
    " | where timestamp > ago(840h)" +
    " | take 3";
var response = await applicationInsightsClient.Query.ExecuteWithHttpMessagesAsync(applicationId, query);

但是,库“Microsoft.Azure.ApplicationInsights.Query”已被弃用,建议使用 Azure.Monitor.Query

下面是 Microsoft 文档中使用 Azure.Monitor.Query 查询日志的示例代码:

Azure.Response<Azure.Monitor.Query.Models.LogsQueryResult> response = 
    await logsQueryClient.QueryWorkspaceAsync(
        "<workspaceId>",
        "customEvents ",
        new QueryTimeRange(TimeSpan.FromMinutes(300)));

由于该库使用工作区 ID 进行查询,因此我将应用程序洞察实例链接到日志分析工作区实例。但是,该函数失败并显示 BadArgumentError“无法解析名为“customEvents”的表或列表达式”

有没有一种方法可以使用 Azure.Monitor.Query 包查询 CustomEvents?

如有任何帮助,我们将不胜感激。

谢谢

c# azure azure-functions azure-application-insights kql
1个回答
2
投票

是的,它有效。
下面是经过测试的代码。

将 Application Insights 链接到 Azure Monitor 工作区后,您可以从该 WS 查询 AI 表,而无需使用 app()
问题是表的名称不同,例如,traces 变为 AppTraces

以同样的方式,customEvents 变为 AppEvents

事实证明,它甚至已记录在迁移到基于工作区的 Application Insights 资源

旧表名称 新表名 描述
可用性结果 应用程序可用性结果 可用性测试的摘要数据。
浏览器计时 应用程序浏览器计时 有关客户端性能的数据,例如处理传入数据所需的时间。
依赖关系 应用程序依赖项 通过 TrackDependency() 记录从应用程序到其他组件(包括外部组件)的调用 – 例如,对 REST API、数据库或文件系统的调用。
自定义事件 应用程序事件 您的应用程序创建的自定义事件。
自定义指标 应用指标 您的应用程序创建的自定义指标。
页面浏览量 应用页面浏览量 有关每个网站视图的数据以及浏览器信息。
性能计数器 应用程序性能计数器 来自支持应用程序的计算资源的性能测量,例如 Windows 性能计数器。
要求 应用程序请求 您的应用程序收到的请求。例如,您的 Web 应用程序收到的每个 HTTP 请求都会记录一个单独的请求记录。
例外情况 应用程序异常 应用程序运行时抛出的异常,捕获服务器端和客户端(浏览器)异常。
痕迹 应用程序痕迹 通过 TrackTrace() 记录的应用程序代码/日志框架发出的详细日志(跟踪)。
using Azure;
using Azure.Identity;
using Azure.Monitor.Query;
using Azure.Monitor.Query.Models;

string workspaceId = "...";

var client = new LogsQueryClient(new DefaultAzureCredential());

try
{
    Response<LogsQueryResult> response = await client.QueryWorkspaceAsync(
        workspaceId,
        "AppEvents | count",
        QueryTimeRange.All);

    LogsTable table = response.Value.Table;

    foreach (var row in table.Rows)
    {
        Console.WriteLine(row);
    }
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}
© www.soinside.com 2019 - 2024. All rights reserved.