我使用azure日志分析.NET SDK来执行一些日志分析查询。
我用于此SDK的金块包是Microsoft.Azure.OperationalInsights。
这允许我发出一些简单的查询,如下面的代码示例所示:
身份验证&简单查询:
partial class QueryProvider
{
private OperationalInsightsDataClient _operationalInsightsDataClient;
private async void Authenticate()
{
// Retrieving the credentials and settings data from the app settings .
var domain = SettingsHelpers.PullSettingsByKey("domain");
var clientId = SettingsHelpers.PullSettingsByKey("clientId");
var workspaceId = SettingsHelpers.PullSettingsByKey("workspaceId");
var authEndpoint = SettingsHelpers.PullSettingsByKey("authEndpoint");
var clientSecret = SettingsHelpers.PullSettingsByKey("clientSecret");
var tokenAudience = SettingsHelpers.PullSettingsByKey("tokenAudience");
// Authenticating to the azure log analytics service .
var azureActiveDirectorySettings = new ActiveDirectoryServiceSettings
{
AuthenticationEndpoint = new Uri(authEndpoint),
TokenAudience = new Uri(tokenAudience),
ValidateAuthority = true
};
var credentials = await ApplicationTokenProvider.LoginSilentAsync
(
domain
, clientId
, clientSecret
, azureActiveDirectorySettings
);
_operationalInsightsDataClient = new OperationalInsightsDataClient(credentials);
_operationalInsightsDataClient.WorkspaceId = workspaceId;
}
public async Task<string> LogAnalyticsSamleQuery()
{
var query = @"Usage
| where TimeGenerated > ago(3h)
| where DataType == 'Perf'
| where QuantityUnit == 'MBytes'
| summarize avg(Quantity) by Computer
| sort by avg_Quantity desc nulls last";
var jsonResult = await _operationalInsightsDataClient.QueryAsync(query);
return JsonConvert.SerializeObject(jsonResult.Results);
}
}
现在我想编写一个运行跨工作区查询的方法,我动态获取工作区ID,并且我想构建引用所有这些工作区的查询。
我没有在doc中找到任何构建此类查询的示例。
我发现了一个名为AdditionalWorkspaces的OperationalInsightDataClient类的属性,但它不清楚如何使用它来实现目标。
非常感谢任何帮助。
答案 0 :(得分:2)
在列表中使用ListWorkspaces方法,存储工作区Id
,CustomerId
或Name
。
var ws = new List<string>();
foreach (var w in workspaces)
{
ws.Add(w.Id);
}
AdditionalWorkspaces
用于存储您要查询的工作空间,但不会影响查询结果。
_operationalInsightsDataClient.AdditionalWorkspaces = ws;
要跨工作区查询,请在查询方法中添加workspace-Id列表。
var jsonResult = await _operationalInsightsDataClient.QueryAsync(query,null, _operationalInsightsDataClient.AdditionalWorkspaces);