简体   繁体   中英

How to create a new instance of Azure Application Insights through the REST API

I'd like to automate the setup of Azure Application Insights accounts for a ASP.Net web application.

I've installed the nuget package: Install-Package Microsoft.Azure.Insights -Pre Now I'm looking at the Microsoft.Azure.Management.Insights.InsightsManagementClient

There are lot's of operations to manage an existing account, except I can't find the one to create a new one.

To be clear: On https://portal.azure.com I can click on New > Create > Developer Services > Application Insights . How do I do that in c#?

Here is the script created by Eric Mattingly (You need Azure PowerShell installed for this to work):

 Output: App Insights Name = erimattestapp IKey = 00000000-0000-0000-0000-000000000000 Script: cls ################################################################## # Set Values ################################################################## #If running manually, comment this out to before the first execution to login to the Azure Portal #Add-AzureAccount #Set the name of the Application Insights Resource $appInsightsName = "erimatTestApp" #Set the application name used for the value of the Tag "AppInsightsApp" - http://azure.microsoft.com/en-us/documentation/articles/azure-preview-portal-using-tags/ $applicationTagName = "erimatTestApp" #Set the name of the Resource Group to use. By default will use the application Name as a starter $resourceGroupName = "erimatTestAppRG" ################################################################## # Create the Resource and Output the name and iKey ################################################################## #Set the script to Resource Manager - http://azure.microsoft.com/en-us/documentation/articles/powershell-azure-resource-manager/ Switch-AzureMode AzureResourceManager #Select the azure subscription Select-AzureSubscription -SubscriptionName "ECIT Preproduction Monitoring" #Create the App Insights Resource $resource = New-AzureResource -Name $appInsightsName -ResourceGroupName $resourceGroupName -Tag @{ Name = "AppInsightsApp"; Value = $applicationTagName} -ResourceType "Microsoft.Insights/Components" -Location "Central US" -ApiVersion "2014-08-01" #Give team owner access - http://azure.microsoft.com/en-us/documentation/articles/role-based-access-control-powershell/ New-AzureRoleAssignment -Mail "ECITTelemetryTeam@microsoft.com" -RoleDefinitionName Owner -Scope $resource.ResourceId | Out-Null #Display iKey Write-Host "App Insights Name = " $resource.Properties["Name"] Write-Host "IKey = " $resource.Properties["InstrumentationKey"] 

Thanks to Anastasia 's powershell example I was able to look at the powershell cmdlet implementation and figure out, how to do this in code:

// initialize resource management client
var resourceManagement = new ResourceManagementClient(this.Credentials);
resourceManagement.Providers.RegisterAsync("microsoft.insights").Result;

// create identity & parameters for create call
var resourceIdentity = new ResourceIdentity(
    "SomeResourceName", // ResourceName
    "microsoft.insights/components", // ResourceType
    "2014-04-01" // Api Version
);
var parameters = new GenericResource {
    Location = 'centralus'
};

// send call off and hope for the best
var result = this.ManagementContext.ResourceManagement.Resources.CreateOrUpdateAsync(
    "SomeResourceGroupName",
    resourceIdentity,
    parameters,
    CancellationToken.None).Result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM