As for getting the name of your instance of App Insights using Instrumentation Key via C #, I was able to build the following program. The Azure SDK documentation is very simple, and NuGet packages are still in preview.
Install NuGet Packages
PM> Install-Package Microsoft.Azure.Management.ApplicationInsights -IncludePrerelease PM> Install-Package Microsoft.Azure.Services.AppAuthentication -IncludePrerelease
Get App Insights Components
using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.Azure.Management.ApplicationInsights.Management; using Microsoft.Azure.Management.ApplicationInsights.Management.Models; using Microsoft.Azure.Services.AppAuthentication; using Microsoft.Rest; namespace CoreConsoleApp { internal class Program { private static async Task Main(string[] args) { // NOTE - see below var auth = new AzureServiceTokenProvider(); const string url = "https://management.azure.com/"; var token = await auth.GetAccessTokenAsync(url); var cred = new TokenCredentials(token); var client = new ApplicationInsightsManagementClient(cred) { SubscriptionId = "<your-subscription-id>", }; var list = new List<ApplicationInsightsComponent>(); var all = await client.Components.ListAsync(); list.AddRange(all); foreach(var item in list) { Console.WriteLine($"{item.Name}: {item.InstrumentationKey}"); } } } }
(Note that you need to use C # 7.1 or later to have async Task Main in your console application).
A note about authentication. The AzureServiceTokenProvider constructor accepts an optional connection string for authentication in Azure. This worked for me without a single one, since I used az login through the Azure CLI . There are quite a few other ways to obtain credentials, some of which are discussed in the Java client documentation .
I am sure there is a more efficient way to request only the InstrumentationKey that you want using the OData query, but I could not figure out how to make this work.
The Microsoft.Azure.Management.ResourceManager package has a more general ResourceManagementClient that allows you to do something like the following:
var client = new Microsoft.Azure.Management.ResourceManager.ResourceManagementClient(cred) { SubscriptionId = "<your-subscription-id>" }; var query = new ODataQuery<GenericResourceFilter>(o => o.ResourceType == "microsoft.insights/components") { Filter = "", // filter by Instrumentation Key here? Expand = "$expand=Properties", }; using (var resp = await client.Resources.ListWithHttpMessagesAsync(query)) { foreach (var item in resp.Body) { Console.WriteLine($"Instance name is {item.Name}"); } }
Finally, this project has several other examples that may be helpful.
source share