简体   繁体   English

如何通过C#获取Azure Log Analytics工作区的工作区ID

[英]How to get the Workspace ID of an Azure Log Analytics workspace via C#

How can I get the Workspace ID of a Log Analytics workspace in Azure via C#? 如何通过C#获取Azure中Log Analytics工作区的工作区ID?

在此处输入图片说明

It seems there is no Log Analytics C# SDK to get the Workspace ID, my workaround is to get the access token vai Microsoft.Azure.Services.AppAuthentication , then call the REST API Workspaces - Get , the customerId in the response is the Workspace ID which you need. 似乎没有Log Analytics C#SDK来获取工作区ID,我的解决方法是获取访问令牌vai Microsoft.Azure.Services.AppAuthentication ,然后调用REST API Workspaces-Get ,响应中的customerId是工作区ID您需要的。

My working sample: 我的工作样本:

using Microsoft.Azure.Services.AppAuthentication;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace ConsoleApp6
{
    class Program
    {

        static void Main(string[] args)
        {
            CallWebAPIAsync().Wait();

        }

        static async Task CallWebAPIAsync()
        {
            AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
            string accessToken = azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/").Result;
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
                client.BaseAddress = new Uri("https://management.azure.com/");


                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                //GET Method  
                HttpResponseMessage response = await client.GetAsync("subscriptions/<subscription id>/resourcegroups/<resource group name>/providers/Microsoft.OperationalInsights/workspaces/<workspace name>?api-version=2015-11-01-preview");
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine(response.Content.ReadAsStringAsync().Result);
                }
                else
                {
                    Console.WriteLine("Internal server Error");
                }
            }
        }
    }
}

在此处输入图片说明

For more details about the authentication, you could take a look at this link . 有关身份验证的更多详细信息,您可以查看此链接

I've since found that the OperationalInsightsManagementClient class can be used as well. 从那以后,我发现还可以使用OperationalInsightsManagementClient类。

var client = new OperationalInsightsManagementClient(GetCredentials()) {SubscriptionId = subscriptionId};
return (await client.Workspaces.ListByResourceGroupWithHttpMessagesAsync(resourceGroupName))
    .Body
    .Select(w => w.CustomerId)
    .FirstOrDefault();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 az CLI 获取 Azure Log Analytics 的工作区 ID? - How to get workspace ID of Azure Log Analytics using az CLI? 如何通过 powershell 将 PostgreSQL 数据库连接到 azure 中的日志分析工作区? - How to connect PostgreSQL database to log analytics workspace in azure via powershell? 获取 Azure 中连接到不同订阅中的工作区的虚拟机的 Log Analytics 工作区 ID - Get Log analytics workspace ID of a virtual machine in Azure connected to a workspace in a different subscription 如何在 Azure Log Analytics 工作区中获取 Windows 安全事件? - How to get Windows Security Events in Azure Log Analytics Workspace? 如何轮换 Azure 日志分析工作区的密钥 - How to rotate keys for azure log analytics workspace 如何通过ARM模板将Azure Activity Monitor连接到Log Analytics Workspace - How to connect Azure Activity Monitor to Log Analytics Workspace via ARM template 如何通过 PowerShell 脚本从 Azure Log Analytics 工作区获取自定义日志表? - How to get Custom Log tables from Azure Log Analytics Workspace through PowerShell script? 是否可以从工作区 ID 获取 OMS Log Analytics 工作区名称 - Is it possible to get the OMS Log Analytics workpace name from the Workspace ID 如何连接 Azure 中现有的自动化帐户和 Log Analytics 工作区? - How to connect existing Automation Account and Log Analytics workspace in Azure? 如何在 azure 门户中永久删除旧的 Log-analytics-workspace? - How to delete old Log-analytics-workspace permanently in azure portal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM