【问题标题】:Cross-workspace queries in azure log analytics .NET SDKAzure 日志分析 .NET SDK 中的跨工作区查询
【发布时间】:2018-10-24 08:21:06
【问题描述】:

我正在使用 Azure 日志分析 .NET SDK 来执行一些日志分析查询。

我用于此 SDK 的 nugget 包是 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,但不清楚如何使用它来实现目标。

任何帮助将不胜感激。

【问题讨论】:

    标签: c# .net azure azure-log-analytics


    【解决方案1】:

    使用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);
    

    【讨论】:

    • 我仍然没有得到 AdditionalWorkspaces 属性的作用,如果需要在每次我们需要运行查询时提供 q 工作区 id QueryAsync 列表
    • @ZackISSER 也许它只是想为要使用的变量提供统一的管理,但实际上没有必要。
    • 嗨,Jerry,有没有一种简单的方法可以获取工作区的文件列表和每个工作区的 SourceComputerId。我需要收集这些数据、每个workspaceId 以及每个workspace 的sourceComputerId 列表。
    猜你喜欢
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2021-06-15
    • 2019-11-02
    相关资源
    最近更新 更多