1

我正在使用 azure log analytics .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,并且我想构建引用所有这些工作区的查询。

我没有在文档中找到任何示例来构建此类查询。

我找到了 OperationalInsightDataClient 类的属性 AdditionalWorkspaces 但不清楚如何使用它来实现目标。

在此处输入图像描述

任何帮助将不胜感激。

4

1 回答 1

2

使用ListWorkspaces方法,存储工作空间IdCustomerIdName在 List 中。

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);
于 2018-05-22T08:01:39.823 回答