Home > OS >  Set Diagnostic Setting for Application Insights
Set Diagnostic Setting for Application Insights

Time:01-06

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?

CodePudding user response:

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
      }
    ]
  }
}

CodePudding user response:

I also got the same error when I executed in my environment.

enter image description here

First of all, if you are trying to enable diagnostic settings(components) for any resource (Eg: Application Insights), Create a new workspace-based application insights and add that resource in the code with the existing keyword as shown below.

resource  component  'Microsoft.Insights/components@2020-02-02'  existing = {
name: 'new'
}

Application Insights:

enter image description here

Refer MsDoc.

And

Verify the list of supported categories as @Thomas has also given, check the prefix and modify accordingly.

I've taken your code and made a few changes and enabled diagnostic settings for application insights successfully.

param workspaceId string
resource  storageAccount  'Microsoft.Storage/storageAccounts@2021-09-01'  existing = {
name: 'newstoragejahnavi'
}
resource  component  'Microsoft.Insights/components@2020-02-02'  existing = {
name: 'new'
}
resource  appdiagnostics  'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
name: 'newsetting'
scope: component
properties: {
storageAccountId: storageAccount.id
workspaceId: workspaceId
logs: [
{
category: 'AppDependencies'
categoryGroup: ''
Retentiondays: 'xx'
enabled: true
}
{
category: 'AppExceptions'
categoryGroup: ''
Retentiondays: 'xx'
enabled: true
}
{
category: 'AppPageViews'
categoryGroup: ''
Retentiondays: 'xx'
enabled: true
}
{
category: 'AppPerformanceCounters'
categoryGroup: ''
Retentiondays: 'xx'
enabled: true
}
{
category: 'AppRequests'
categoryGroup: ''
Retentiondays: 'xx'
enabled: true
}
{
category: 'AppSystemEvents'
categoryGroup: ''
Retentiondays: 'xx'
enabled: true
}
{
category: 'AppTraces'
categoryGroup: ''
Retentiondays: 'xx'
enabled: true
}
]
metrics: [
{
category: 'Allmetrics'
enabled: true
}
]
}
}

Deployment succeeded:

enter image description here

Output:

enter image description here

  • Related