azureazure-application-insightsazure-resource-managerazure-bicepazure-diagnostics

Set Diagnostic Setting for Application Insights


I have created a diagnostic setting for a Log Analytics Workspace.

I'd also like to create and automate a diagnostic setting for workspace-based in Application Insights. However, I am not sure if that makes sense.

I have created a diagnostic setting as followed:

resource appDiagnostics 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
  name: appSettingName
  scope: applicationInsights
  properties: {
    storageAccountId: storageAccountId
    logs: [
      {
        category: 'Metrics'
        enabled: true
      }
      {
        category: 'Dependencies'
        enabled: true
      }
      {
        category: 'Exceptions'
        enabled: true
      }
      {
        category: 'PageViews'
        enabled: true
      }
      {
        category: 'PerformanceCounters'
        enabled: true
      }
      {
        category: 'Requests'
        enabled: true
      }
      {
        category: 'SystemEvents'
        enabled: true
      }
      {
        category: 'Traces'
        enabled: true
      }
    ]
    metrics: [
      {
        category: 'AllMetrics'
        enabled: true
      }
    ]
  }
}

However, I always get an error that my log categories are not valid. Is it because the data is double?


Solution

  • If you look at Azure Monitor Logs references, you will see that all log names have the App prefix so you should be able to do something like that:

    var logTypes = [
      'AppMetrics'
      'AppDependencies'
      'AppExceptions'
      'AppPageViews'
      'AppPerformanceCounters'
      'AppRequests'
      'AppSystemEvents'
      'AppTraces'
      'AppAvailabilityResults'
      'AppBrowserTimings'
      'AppEvents'
    ]
    
    resource appDiagnostics 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
      name: appSettingName
      scope: applicationInsights
      properties: {
        storageAccountId: storageAccountId
        logs: [for logType in logTypes: {
          category: logType
          enabled: true
        }]
        metrics: [
          {
            category: 'AllMetrics'
            enabled: true
          }
        ]
      }
    }