Search code examples
powershellazure-web-app-serviceazure-application-insightsazure-resource-managerazure-powershell

Enable .NET Core collection in AppService with ARM or PowerShell


I would like to enable the .NET Core collection level with applicationInsight in Azure AppService as shown in below picture. From Azure Portal it works well. Keep in mind that by default value is set to disabled:

enter image description here

Now I would like to automate this using either ARM template or powershell.

  • I did a export template to see how it looks on ARM but there is no settings related to .NET Core collection
  • I check documentation on MS website but nothing about ARM template with enabling collection too
  • In PowerShell same problem

Is there anyone in the community who know how to enable the collection using ARM or PowerShell ?

Thanks a lot !


Solution

  • It's simply setting an app setting called XDT_MicrosoftApplicationInsights_Mode with value recommended (to enable) or default (to disable) as described here. You can do that both in ARM or PowerShell as below.

    ARM (check appSettings part):

    {
        "resources": [
            {
                "name": "[parameters('name')]",
                "type": "Microsoft.Web/sites",
                "properties": {
                    "siteConfig": {
                        "appSettings": [
                            {
                                "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                                "value": "[reference('microsoft.insights/components/AppMonitoredSite', '2015-05-01').InstrumentationKey]"
                            },
                            {
                                "name": "APPLICATIONINSIGHTS_CONNECTION_STRING",
                                "value": "[reference('microsoft.insights/components/AppMonitoredSite', '2015-05-01').ConnectionString]"
                            },
                            {
                                "name": "ApplicationInsightsAgent_EXTENSION_VERSION",
                                "value": "~2"
                            },
                            {
                                "name": "XDT_MicrosoftApplicationInsights_Mode ",
                                "value": "recommended"
                            }
                        ]
                    },
                    "name": "[parameters('name')]",
                    "serverFarmId": "[concat('/subscriptions/', parameters('subscriptionId'),'/resourcegroups/', parameters('serverFarmResourceGroup'), '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
                    "hostingEnvironment": "[parameters('hostingEnvironment')]"
                },
                "dependsOn": [
                    "[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
                    "microsoft.insights/components/AppMonitoredSite"
                ],
                "apiVersion": "2016-03-01",
                "location": "[parameters('location')]"
            },
            {
                "apiVersion": "2016-09-01",
                "name": "[parameters('hostingPlanName')]",
                "type": "Microsoft.Web/serverfarms",
                "location": "[parameters('location')]",
                "properties": {
                    "name": "[parameters('hostingPlanName')]",
                    "workerSizeId": "[parameters('workerSize')]",
                    "numberOfWorkers": "1",
                    "hostingEnvironment": "[parameters('hostingEnvironment')]"
                },
                "sku": {
                    "Tier": "[parameters('sku')]",
                    "Name": "[parameters('skuCode')]"
                }
            },
            {
                "apiVersion": "2015-05-01",
                "name": "AppMonitoredSite",
                "type": "microsoft.insights/components",
                "location": "West US 2",
                "properties": {
                    "ApplicationId": "[parameters('name')]",
                    "Request_Source": "IbizaWebAppExtensionCreate"
                }
            }
        ],
        "parameters": {
            "name": {
                "type": "string"
            },
            "hostingPlanName": {
                "type": "string"
            },
            "hostingEnvironment": {
                "type": "string"
            },
            "location": {
                "type": "string"
            },
            "sku": {
                "type": "string"
            },
            "skuCode": {
                "type": "string"
            },
            "workerSize": {
                "type": "string"
            },
            "serverFarmResourceGroup": {
                "type": "string"
            },
            "subscriptionId": {
                "type": "string"
            }
        },
        "$schema": "https://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0"
    }
    

    Powershell:

    $app = Get-AzWebApp -ResourceGroupName "AppMonitoredRG" -Name "AppMonitoredSite" -ErrorAction Stop
    $newAppSettings = @{} # case-insensitive hash map
    $app.SiteConfig.AppSettings | %{$newAppSettings[$_.Name] = $_.Value} # preserve non Application Insights application settings.
    $newAppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"] = "012345678-abcd-ef01-2345-6789abcd"; # set the Application Insights instrumentation key
    $newAppSettings["APPLICATIONINSIGHTS_CONNECTION_STRING"] = "InstrumentationKey=012345678-abcd-ef01-2345-6789abcd"; # set the Application Insights connection string
    $newAppSettings["ApplicationInsightsAgent_EXTENSION_VERSION"] = "~2"; # enable the ApplicationInsightsAgent
    $newAppSettings["XDT_MicrosoftApplicationInsights_Mode"] = "recommended"; # set the APM collection to recommended
    $app = Set-AzWebApp -AppSettings $newAppSettings -ResourceGroupNamrecommendede $app.ResourceGroup -Name $app.Name -ErrorAction Stop