4
votes

I am trying to obtain the instrumentation key and api key required to configure Application Insights in my application programmatically.

I am doing this because I have a number of applications each with separate instances of Application Insights which need to be configured independently.

I have attempted to use Microsoft.Azure.Management.ResourceManager to obtain the resource on Azure but I cannot see a way to get or set the required information.

This is what I have so far

ServiceClientCredentials credentials = await AuthenticationManagement.GetToken();

using (var resourceClient = new ResourceManagementClient(credentials))
{
    resourceClient.SubscriptionId = AuthenticationManagement.SubscriptionId;

    foreach (var item in resourceClient.Resources.List(new ODataQuery<GenericResourceFilter>(o => o.ResourceType == "microsoft.insights/components")
    {
        Expand = "$expand=Properties"
    }))
    {
        Console.WriteLine(item.Properties); //Still empty?
    }

}

I've gotten stuck at this point because I see no obvious method for updating or retrieving the values I need from the API.

So could anyone provide any guidance on how I might be able to retrieve this information using the ResourceManagementClient?

1

1 Answers

3
votes

Finally worked it out. The class I was looking for was in Microsoft.Azure.Management.ApplicationInsights there you will find ApplicationInsightsManagementClient which allows you to create a web api key and retrieve further details about your insights service. The nuget package can be found here.

This is what the code looks like

using (var appInsightsManagementClient = new ApplicationInsightsManagementClient(credentials))
{
    ServiceClientCredentials credentials = await AuthenticationManagement.GetToken();

    appInsightsManagementClient.SubscriptionId = AuthenticationManagement.SubscriptionId;

    appInsightsManagementClient.APIKeys.Create("Resource-Group", "Resource-Name", new APIKeyRequest());
}