getting LoggerId must be specified., Target= loggerId forAzApiManagementDiagnostic

Saini, Rominder 0 Reputation points
2024-12-02T17:35:52.7066667+00:00

getting the below error for New-AzApiManagementDiagnostic: 

Error Details:         [Code= ValidationError, Message= LoggerId must be specified., Target= loggerId]
below is the code have replaced ResourceGroupName,ServiceName,LoggerId with dummy value to paste the code here

$context = New-AzApiManagementContext -ResourceGroupName "XXX" -ServiceName "XXXX-apim"

$logger = Get-AzApiManagementLogger -Context $context -LoggerId "XXX-ins"

$samplingsetting = New-AzApiManagementSamplingSetting -SamplingType fixed -SamplingPercentage 100

New-AzApiManagementDiagnostic -Context $context -loggerId $logger.loggerId -AlwaysLog allErrors -SamplingSetting $samplingSetting -DiagnosticId "applicationinsights"

Error
New-AzApiManagementDiagnostic:  Error Code: ValidationError Error Message: One or more fields contain incorrect values: Request Id: ddbd86a8-fe4c-4aaa-9b0c-b844eaf213d9 Error Details:         [Code= ValidationError, Message= LoggerId must be specified., Target= loggerId]

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,262 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 29,561 Reputation points Microsoft Employee
    2024-12-03T01:07:20.58+00:00

    Hi @Saini, Rominder

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.