英文:
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,但它看起来很旧,所以我想知道这是否仍然有效。
所以我很困惑,你能帮我找到正确的方法吗?我的要求是不在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.
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<IReadOnlyList<T>> QueryAsync<T>(string query, TimeSpan timeSpan)
{
var response = await _client.QueryWorkspaceAsync<T>(_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<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();
}
where we have a CustomEvent
(in application insights) which becomes an AppEvent
in Workspace, which has been created from a docker container.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论