Inspect the $logger
object. Based on the error message, the LoggerId
isn't set in that object. In my case, I just ran Get-AzApiManagementLogger -Context $context
just to get the list of configured loggers. Furthermore, you want to make sure that logger id is configured for applicationinsights
. Failure to this will result in a validation error.
EDIT 13 Dec 2024 Thanks for discussing the issue through private message. By using the -Debug
switch, we saw that New-AzApiManagementDiagnostic
is not including LoggerId in the request body payload. Since I wasn't encountering this error, I checked my version of Azure PowerShell and I'm running v12.0.0. I would make sure you're running that version or later. In the interim, you can use Invoke-AzRestMethod to create the new diagnostic resource.
$subscriptionId = "<YourSubscriptionId>"
$resourceGroupName = "YourResourceGroupName"
$serviceName = "XXXX-apim"
$diagnosticId = "applicationinsights"
$apiVersion = "2021-08-01"
$url = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.ApiManagement/service/$serviceName/diagnostics/$diagnosticId?api-version=$apiVersion"
$payload = @{
properties = @{
alwaysLog = "allErrors"
loggerId = "/loggers/$loggerId"
sampling = @{
samplingType = "fixed"
percentage = 100
}
}
} | ConvertTo-Json
Invoke-AzRestMethod -Method PUT -Uri $url -Body $payload -ContentType "application/json"
EDIT 8 Jan 2025 Per our offline discussions, I went back to used ran Azure PowerShell in docker using 12.1.0 and 12.4.0 with the script below. I still couldn't reproduce the missing logger Id error with either one. Therefore, these two along with 12.0.0 should work.
$apimGroup = "YourApimResourceGroupName"
$apimService = "XXXX-apim"
$appInsights = "XXXX-ai"
$appInsightsGroup = "YourAppInsightsResourceGroup"
$context = New-AzApiManagementContext -ResourceGroupName $apimGroup -ServiceName $apimService
$appInsights = Get-AzApplicationInsights -Name $appInsights -ResourceGroupName $appInsightsGroup
# this line is commented because Get-AzApplicationInsights doesn't return the instrumentation key
# $logger = New-AzApiManagementLogger -Context $context -InstrumentationKey $appInsights.InstrumenationKey
$logger = Get-AzApiManagementLogger -Context $context -LoggerId $appInsights.Name
$sampleSetting = New-AzApiManagementSamplingSetting -SamplingPercentage 100 -SamplingType fixed
$logger
New-AzApiManagementDiagnostic -Context $context -LoggerId $logger.LoggerId -AlwaysLog AllErrors -SamplingSetting $sampleSetting -DiagnosticId ApplicationInsights
However, if you're still hitting this error with either of these or the latest versions of Az PowerShell, then I strongly suggest creating an issue on the GitHub repo.