如何使用C#从Azure应用洞察中提取记录?

huangapple go评论60阅读模式
英文:

How to fetch records from the azure application insights using C#?

问题

我需要使用C#从Azure应用洞察中获取记录。

我尝试过下面的方法,但它需要在Azure中创建一个应用服务,我想避免这样做。

https://learn.microsoft.com/en-us/azure/azure-monitor/app/app-insights-azure-ad-api

我还按照这个答案,OP建议使用https://www.nuget.org/packages/Azure.Monitor.Query,但它看起来很旧,所以我想知道这是否仍然有效。

https://stackoverflow.com/questions/64100878/alternative-to-https-www-nuget-org-packages-microsoft-azure-applicationinsight

所以我很困惑,你能帮我找到正确的方法吗?我的要求是不在Azure中创建任何额外的资源,并希望使用C#从应用洞察中获取数据。

英文:

I need to fetch records from Azure application insights using C#.

I have tried using the below approach, but it requires an app service to be created in Azure and I want to avoid it.

https://learn.microsoft.com/en-us/azure/azure-monitor/app/app-insights-azure-ad-api

I have also followed this answer as well, OP has suggested using https://www.nuget.org/packages/Azure.Monitor.Query but it looks very old, so I would like to know if is this still valid.

https://stackoverflow.com/questions/64100878/alternative-to-https-www-nuget-org-packages-microsoft-azure-applicationinsight

So I am confused, Could you please help me with the correct approach? My requirement is not to create any extra resources in Azure and wants to fetch from app insights using C#.

答案1

得分: 1

如果您已经配置了 Application Insights,只要您配置了工作区,就可以查询数据。

例如,一旦配置了工作区,可以使用以下通用方法来针对 WorkspaceId 进行查询:

private async Task<IReadOnlyList<T>> QueryAsync<T>(string query, TimeSpan timeSpan)
{
    var response = await _client.QueryWorkspaceAsync<T>(_options.WorkspaceId, query, new QueryTimeRange(timeSpan));

    return response.Value;
}

其中 _client 是:

Azure.Montitor.Query.LogsQueryClient _client;

一个示例查询可能如下所示:

public async Task<int> GetFailedViewCount()
{
    var query = $"AppEvents" +
        $"| extend Output = tostring(Properties[\"Output\"]) " +
        $"| where Output contains (\"Failed to execute internal view\")" +
        $"| summarize Count=count()";

    var response = await QueryAsync<int>(query, TimeSpan.FromHours(48));

    return response.FirstOrDefault();
}

在这里,我们有一个在应用程序洞察中的 CustomEvent,它在工作区中变成了一个 AppEvent,并且是从 Docker 容器创建的。

英文:

If you already have Application Insights configured, you can query the data as long as you configure a workspace.

For example, once a workspace is configured, a generic method like below can be used to query against a WorkspaceId:

private async Task&lt;IReadOnlyList&lt;T&gt;&gt; QueryAsync&lt;T&gt;(string query, TimeSpan timeSpan)
{
    var response = await _client.QueryWorkspaceAsync&lt;T&gt;(_options.WorkspaceId, query, new QueryTimeRange(timeSpan));

    return response.Value;
}

where _client is:

Azure.Montitor.Query.LogsQueryClient _client;

An example query could be as such:

public async Task&lt;int&gt; GetFailedViewCount()
{
    var query = $&quot;AppEvents&quot; +
        $&quot;| extend Output = tostring(Properties[\&quot;Output\&quot;]) &quot; +
        $&quot;| where Output contains (\&quot;Failed to execute internal view\&quot;)&quot; +
        $&quot;| summarize Count=count()&quot;;

    var response = await QueryAsync&lt;int&gt;(query, TimeSpan.FromHours(48));

    return response.FirstOrDefault();
}

where we have a CustomEvent (in application insights) which becomes an AppEvent in Workspace, which has been created from a docker container.

huangapple
  • 本文由 发表于 2023年6月22日 17:54:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定