在蔚蓝的日志分析.NET SDK跨工作区查询(Cross-workspace queries in

2019-09-25 18:26发布

我使用的是蔚蓝的日志分析.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的属性,但目前还不清楚如何使用它要达到的目标。

任何帮助将是非常赞赏。

Answer 1:

使用ListWorkspaces方法,店内工作区IdCustomerIdName的列表。

var ws = new List<string>();
foreach (var w in workspaces)
{
     ws.Add(w.Id);
}

AdditionalWorkspaces是用来存储要查询的工作空间,但对查询结果没有影响。

_operationalInsightsDataClient.AdditionalWorkspaces = ws;

交叉工作区的查询,添加查询方法中的工作区-ID列表。

var jsonResult = await _operationalInsightsDataClient.QueryAsync(query,null, _operationalInsightsDataClient.AdditionalWorkspaces);


文章来源: Cross-workspace queries in azure log analytics .NET SDK