Application Insights for Azure VMs and virtual machine scale sets

Enabling monitoring for your ASP.NET and ASP.NET Core IIS-hosted applications running on Azure Virtual Machines or Azure Virtual Machine Scale Sets is now easier than ever. Get all the benefits of using Application Insights without modifying your code.

This article walks you through enabling Application Insights monitoring by using the Application Insights Agent. It also provides preliminary guidance for automating the process for large-scale deployments.

Enable Application Insights

Auto-instrumentation is easy to enable. Advanced configuration isn't required.

For a complete list of supported auto-instrumentation scenarios, see Supported environments, languages, and resource providers.

Note

Auto-instrumentation is available for ASP.NET, ASP.NET Core IIS-hosted applications, and Java. Use an SDK to instrument Node.js and Python applications hosted on Azure virtual machines and virtual machine scale sets.

The Application Insights Agent autocollects the same dependency signals out of the box as the SDK. To learn more, see Dependency autocollection.

Before you install the Application Insights Agent extension, you'll need a connection string. Create a new Application Insights resource or copy the connection string from an existing Application Insights resource.

Enable monitoring for virtual machines

You can use the Azure portal or PowerShell to enable monitoring for VMs.

Azure portal

  1. In the Azure portal, go to your Application Insights resource. Copy your connection string to the clipboard.

    Screenshot that shows the connection string.

  2. Go to your virtual machine. Under the Settings section in the menu on the left side, select Extensions + applications > Add.

    Screenshot that shows the Extensions + applications pane with the Add button.

  3. Select Application Insights Agent > Next.

    Screenshot that shows the Install an Extension pane with the Next button.

  4. Paste the connection string you copied in step 1 and select Review + create.

    Screenshot that shows the Create tab with the Review + create button.

PowerShell

Note

Are you new to PowerShell? Check out the Get started guide.

Install or update the Application Insights Agent as an extension for Azure virtual machines:

# define variables to match your environment before running
$ResourceGroup = "<myVmResourceGroup>"
$VMName = "<myVmName>"
$Location = "<myVmLocation>"
$ConnectionString = "<myAppInsightsResourceConnectionString>"

$publicCfgJsonString = @"
{
    "redfieldConfiguration": {
        "instrumentationKeyMap": {
        "filters": [
            {
            "appFilter": ".*",
            "machineFilter": ".*",
            "virtualPathFilter": ".*",
            "instrumentationSettings" : {
                "connectionString": "$ConnectionString"
            }
            }
        ]
        }
    }
    }
"@

$privateCfgJsonString = '{}'
	
Set-AzVMExtension -ResourceGroupName $ResourceGroup -VMName $VMName -Location $Location -Name "ApplicationMonitoringWindows" -Publisher "Microsoft.Azure.Diagnostics" -Type "ApplicationMonitoringWindows" -Version "2.8" -SettingString $publicCfgJsonString -ProtectedSettingString $privateCfgJsonString

Note

For more complicated at-scale deployments, you can use a PowerShell loop to install or update the Application Insights Agent extension across multiple VMs.

Query the Application Insights Agent extension status for Azure virtual machines:

Get-AzVMExtension -ResourceGroupName "<myVmResourceGroup>" -VMName "<myVmName>" -Name ApplicationMonitoringWindows -Status

Get a list of installed extensions for Azure virtual machines:

Get-AzResource -ResourceId "/subscriptions/<mySubscriptionId>/resourceGroups/<myVmResourceGroup>/providers/Microsoft.Compute/virtualMachines/<myVmName>/extensions"

Uninstall the Application Insights Agent extension from Azure virtual machines:

Remove-AzVMExtension -ResourceGroupName "<myVmResourceGroup>" -VMName "<myVmName>" -Name "ApplicationMonitoring"

Note

Verify installation by selecting Live Metrics Stream within the Application Insights resource associated with the connection string you used to deploy the Application Insights Agent extension. If you're sending data from multiple virtual machines, select the target Azure virtual machines under Server Name. It might take up to a minute for data to begin flowing.

Enable monitoring for virtual machine scale sets

You can use the Azure portal or PowerShell to enable monitoring for virtual machine scale sets.

Azure portal

Follow the prior steps for VMs, but go to your virtual machine scale sets instead of your VM.

PowerShell

Install or update Application Insights Agent as an extension for virtual machine scale sets:

# Set resource group, vmss name, and connection string to reflect your environment
$ResourceGroup = "<myVmResourceGroup>"
$VMSSName = "<myVmName>"
$ConnectionString = "<myAppInsightsResourceConnectionString>"
$publicCfgHashtable =
@{
  "redfieldConfiguration"= @{
    "instrumentationKeyMap"= @{
      "filters"= @(
        @{
          "appFilter"= ".*";
          "machineFilter"= ".*";
          "virtualPathFilter"= ".*";
          "instrumentationSettings" = @{
            "connectionString"= "$ConnectionString"
          }
        }
      )
    }
  }
};
$privateCfgHashtable = @{};
$vmss = Get-AzVmss -ResourceGroupName $ResourceGroup -VMScaleSetName $VMSSName
Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name "ApplicationMonitoringWindows" -Publisher "Microsoft.Azure.Diagnostics" -Type "ApplicationMonitoringWindows" -TypeHandlerVersion "2.8" -Setting $publicCfgHashtable -ProtectedSetting $privateCfgHashtable
Update-AzVmss -ResourceGroupName $vmss.ResourceGroupName -Name $vmss
# Note: Depending on your update policy, you might need to run Update-AzVmssInstance for each instance

Get a list of installed extensions for virtual machine scale sets:

Get-AzResource -ResourceId "/subscriptions/<mySubscriptionId>/resourceGroups/<myResourceGroup>/providers/Microsoft.Compute/virtualMachineScaleSets/<myVmssName>/extensions"

Uninstall the application monitoring extension from virtual machine scale sets:

# set resource group and vmss name to reflect your environment
$vmss = Get-AzVmss -ResourceGroupName "<myResourceGroup>" -VMScaleSetName "<myVmssName>"
Remove-AzVmssExtension -VirtualMachineScaleSet $vmss -Name "ApplicationMonitoringWindows"
Update-AzVmss -ResourceGroupName $vmss.ResourceGroupName -Name $vmss.Name -VirtualMachineScaleSet $vmss
# Note: Depending on your update policy, you might need to run Update-AzVmssInstance for each instance

Troubleshooting

Find troubleshooting tips for the Application Insights Monitoring Agent extension for .NET applications running on Azure virtual machines and virtual machine scale sets.

If you're having trouble deploying the extension, review the execution output that's logged to files found in the following directories:

C:\WindowsAzure\Logs\Plugins\Microsoft.Azure.Diagnostics.ApplicationMonitoringWindows\<version>\

If your extension deployed successfully but you're unable to see telemetry, it could be one of the following issues covered in Agent troubleshooting:

  • Conflicting DLLs in an app's bin directory
  • Conflict with IIS shared configuration

Test connectivity between your application host and the ingestion service

Application Insights SDKs and agents send telemetry to get ingested as REST calls to our ingestion endpoints. You can test connectivity from your web server or application host machine to the ingestion service endpoints by using raw REST clients from PowerShell or curl commands. See Troubleshoot missing application telemetry in Azure Monitor Application Insights.

Release notes

2.8.44

  • Updated Application Insights .NET/.NET Core SDK to 2.20.1 - red field.
  • Enabled SQL query collection.
  • Enabled support for Azure Active Directory authentication.

2.8.42

Updated Application Insights .NET/.NET Core SDK to 2.18.1 - red field.

2.8.41

Added the ASP.NET Core auto-instrumentation feature.

Next steps