Sam*_*Sam 4 azure azure-application-insights azure-cli azure-webapps
考虑以下代码。它创建一个应用程序洞察,然后它检索检测密钥并将其分配给我的 web 应用程序。
az monitor app-insights component create -g $resourceGroup --app $webapp --application-type web --kind web --tags $defaultTags
$instrumentationKey = az monitor app-insights component show -g $resourceGroup -a $webapp --query 'instrumentationKey' -o tsv
az webapp config appsettings set -g $resourceGroup -n $webapp --settings APPINSIGHTS_INSTRUMENTATIONKEY=$instrumentationKey APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=$instrumentationKey
Run Code Online (Sandbox Code Playgroud)
但是,这不会打开此屏幕截图中所示的 web 应用程序的应用程序洞察力。我不知道如何从 azure cli 打开它。
您需要设置更多应用程序设置,以使其与从 Azure 门户启用它完全一样。我相信仪器键之后的第二个重要键是ApplicationInsightsAgent_EXTENSION_VERSION。
您可以适应AzureCLI的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
$app = Set-AzWebApp -AppSettings $newAppSettings -ResourceGroupName $app.ResourceGroup -Name $app.Name -ErrorAction Stop
Run Code Online (Sandbox Code Playgroud)
使用链接@Alex AIT 提供的 cli 可能如下所示。
请注意,您还可以依赖这样一个事实:如果您不创建 App Insights 实例,则会创建和使用自动实例。
# (...) Set up $plan, $resourcegroup and $region
az appservice plan create --name $plan --resource-group $resourcegroup --location $region --sku FREE
[String]$webapp="myapp"
az webapp create --name $webapp --plan $plan --resource-group $resourcegroup
[String]$appinsights=$webapp
az monitor app-insights component create --app $appinsights --location $region --resource-group $resourcegroup
# Get the instrumentation key
# '--output tsv', which is 'Tab-separated values, with no keys'
# is used to obtain the unquoted value
# as shown in https://docs.microsoft.com/en-us/cli/azure/query-azure-cli?view=azure-cli-latest#get-a-single-value
[String]$instrumentationKey = (az monitor app-insights component show --app $appinsights --resource-group $resourcegroup --query "instrumentationKey" --output tsv)
# Configure the app to use new app insights instance
# Based on https://docs.microsoft.com/en-us/azure/azure-monitor/app/azure-web-apps?tabs=net#enabling-through-powershell
az webapp config appsettings set --name $webapp --resource-group $resourcegroup --settings APPINSIGHTS_INSTRUMENTATIONKEY=$instrumentationKey APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=$instrumentationKey ApplicationInsightsAgent_EXTENSION_VERSION=~2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1246 次 |
| 最近记录: |