You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ARM模板部署Function App并配置Diagnostic Settings至Log Analytics的问题

ARM模板部署Log Analytics工作区、Function App并关联诊断设置

完整ARM模板示例

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "resourceGroupName": {
      "type": "string",
      "metadata": {
        "description": "资源组名称"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "资源部署位置"
      }
    },
    "logAnalyticsWorkspaceName": {
      "type": "string",
      "metadata": {
        "description": "Log Analytics工作区名称"
      }
    },
    "functionAppName": {
      "type": "string",
      "metadata": {
        "description": "Function App名称"
      }
    },
    "appServicePlanName": {
      "type": "string",
      "metadata": {
        "description": "App Service计划名称"
      }
    },
    "diagnosticSettingName": {
      "type": "string",
      "defaultValue": "FunctionApp-To-LogAnalytics",
      "metadata": {
        "description": "诊断设置名称"
      }
    },
    "functionsWorkerRuntime": {
      "type": "string",
      "allowedValues": [
        "dotnet",
        "node",
        "python",
        "java"
      ],
      "metadata": {
        "description": "Function App运行时"
      }
    }
  },
  "variables": {},
  "resources": [
    {
      "type": "Microsoft.OperationalInsights/workspaces",
      "apiVersion": "2021-06-01",
      "name": "[parameters('logAnalyticsWorkspaceName')]",
      "location": "[parameters('location')]",
      "properties": {
        "sku": {
          "name": "PerGB2018"
        },
        "retentionInDays": 30
      }
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2022-03-01",
      "name": "[parameters('appServicePlanName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Y1",
        "tier": "Dynamic",
        "size": "Y1",
        "family": "Y",
        "capacity": 0
      },
      "kind": "functionapp",
      "properties": {
        "reserved": false
      }
    },
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2022-03-01",
      "name": "[parameters('functionAppName')]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName'))]"
      ],
      "kind": "functionapp",
      "properties": {
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName'))]",
        "siteConfig": {
          "appSettings": [
            {
              "name": "FUNCTIONS_EXTENSION_VERSION",
              "value": "~4"
            },
            {
              "name": "FUNCTIONS_WORKER_RUNTIME",
              "value": "[parameters('functionsWorkerRuntime')]"
            },
            {
              "name": "AzureWebJobsStorage",
              "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', parameters('functionAppName'), 'storage;AccountKey=', listKeys(resourceId('Microsoft.Storage/storageAccounts', concat(parameters('functionAppName'), 'storage')), '2021-04-01').keys[0].value, ';EndpointSuffix=core.windows.net')]"
            }
          ]
        }
      },
      "resources": [
        {
          "type": "storageAccounts",
          "apiVersion": "2021-04-01",
          "name": "[concat(parameters('functionAppName'), 'storage')]",
          "location": "[parameters('location')]",
          "sku": {
            "name": "Standard_LRS"
          },
          "kind": "StorageV2"
        }
      ]
    },
    {
      "type": "Microsoft.Web/sites/providers/diagnosticSettings",
      "apiVersion": "2021-05-01-preview",
      "name": "[concat(parameters('functionAppName'), '/Microsoft.Insights/', parameters('diagnosticSettingName'))]",
      "dependsOn": [
        "[resourceId('Microsoft.Web/sites', parameters('functionAppName'))]",
        "[resourceId('Microsoft.OperationalInsights/workspaces', parameters('logAnalyticsWorkspaceName'))]"
      ],
      "properties": {
        "workspaceId": "[resourceId('Microsoft.OperationalInsights/workspaces', parameters('logAnalyticsWorkspaceName'))]",
        "logs": [
          {
            "category": "FunctionAppLogs",
            "enabled": true
          },
          {
            "category": "AppServiceHTTPLogs",
            "enabled": true
          },
          {
            "category": "AppServiceConsoleLogs",
            "enabled": true
          },
          {
            "category": "AppServiceAppLogs",
            "enabled": true
          }
        ],
        "metrics": [
          {
            "category": "AllMetrics",
            "enabled": true
          }
        ]
      }
    }
  ]
}

核心配置要点

  • Function App运行时版本:必须在appSettings中设置FUNCTIONS_EXTENSION_VERSION为~4(或更高支持版本),~1版本不支持诊断设置,这是门户菜单灰显的根源。
  • 诊断设置关联方式:使用Microsoft.Web/sites/providers/diagnosticSettings资源类型,而非单独的Microsoft.Insights/diagnosticSettings,资源名称格式为[FunctionApp名称]/Microsoft.Insights/[诊断设置名称],确保诊断设置直接绑定到Function App,避免出现“游离”问题。
  • 依赖关系控制:诊断设置必须依赖Function App和Log Analytics工作区部署完成,通过dependsOn明确指定,防止因资源未就绪导致关联失效。
  • 日志类别配置:示例包含Function App常用日志类型,可根据实际需求调整启用的日志/指标类别。

部署验证

部署完成后:

  1. 进入Function App的诊断设置菜单,确认配置的诊断设置已存在且状态为启用。
  2. 进入Log Analytics工作区,通过查询FunctionAppLogs等表验证日志是否正常流入。

内容的提问来源于stack exchange,提问作者Adrián Alvarez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 22:08:18