簡體   English   中英

如何通過REST API創建Azure Application Insights的新實例

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

我想自動為ASP.Net Web應用程序設置Azure Application Insights帳戶。

我已經安裝了nuget包: Install-Package Microsoft.Azure.Insights -Pre現在我正在查看Microsoft.Azure.Management.Insights.InsightsManagementClient

管理現有帳戶有很多操作,但我找不到創建新帳戶的操作。

需要說明的是:在https://portal.azure.com上,我可以單擊“ New > Create > Developer Services > Application Insights 我怎么用c#做到這一點?

這是由Eric Mattingly創建的腳本(為此需要安裝Azure PowerShell):

 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"] 

感謝Anastasiapowershell示例,我能夠查看powershell cmdlet實現並弄清楚如何在代碼中執行此操作:

// 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM