您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

Azure Functions 2.x 的 host.json 参考host.json reference for Azure Functions 2.x

host.json 元数据文件包含对函数应用的所有函数产生影响的全局配置选项。The host.json metadata file contains global configuration options that affect all functions for a function app. 本文列出了可用于 v2 运行时的设置。This article lists the settings that are available for the v2 runtime.

备注

本文适用于 Azure Functions 2.x。This article is for Azure Functions 2.x. 有关 Functions 1.x 中 host.json 的参考,请参阅 Azure Functions 1.x 的 host.json 参考For a reference of host.json in Functions 1.x, see host.json reference for Azure Functions 1.x.

其他函数应用配置选项是在你的应用设置中管理的。Other function app configuration options are managed in your app settings.

local.settings.json 文件中的某些 host.json 设置仅在本地运行时才使用。Some host.json settings are only used when running locally in the local.settings.json file.

示例 host.json 文件Sample host.json file

以下示例 host.json 文件指定了所有可能的选项。The following sample host.json files have all possible options specified.

{
    "version": "2.0",
    "aggregator": {
        "batchSize": 1000,
        "flushTimeout": "00:00:30"
    },
    "extensions": {
        "cosmosDb": {},
        "durableTask": {},
        "eventHubs": {},
        "http": {},
        "queues": {},
        "sendGrid": {},
        "serviceBus": {}
    },
    "functions": [ "QueueProcessor", "GitHubWebHook" ],
    "functionTimeout": "00:05:00",
    "healthMonitor": {
        "enabled": true,
        "healthCheckInterval": "00:00:10",
        "healthCheckWindow": "00:02:00",
        "healthCheckThreshold": 6,
        "counterThreshold": 0.80
    },
    "logging": {
        "fileLoggingMode": "debugOnly",
        "logLevel": {
          "Function.MyFunction": "Information",
          "default": "None"
        },
        "applicationInsights": {
            "samplingSettings": {
              "isEnabled": true,
              "maxTelemetryItemsPerSecond" : 5
            }
        }
    },
    "singleton": {
      "lockPeriod": "00:00:15",
      "listenerLockPeriod": "00:01:00",
      "listenerLockRecoveryPollingInterval": "00:01:00",
      "lockAcquisitionTimeout": "00:01:00",
      "lockAcquisitionPollingInterval": "00:00:03"
    },
    "watchDirectories": [ "Shared", "Test" ],
    "managedDependency": {
        "enabled": true
    }
}

本文的以下各部分解释了每个顶级属性。The following sections of this article explain each top-level property. 除非另有说明,否则其中的所有属性都是可选的。All are optional unless otherwise indicated.

aggregatoraggregator

指定在计算 Application Insights 的指标时要聚合多少个函数调用。Specifies how many function invocations are aggregated when calculating metrics for Application Insights.

{
    "aggregator": {
        "batchSize": 1000,
        "flushTimeout": "00:00:30"
    }
}
属性Property 默认Default 描述Description
batchSizebatchSize 10001000 要聚合的最大请求数。Maximum number of requests to aggregate.
flushTimeoutflushTimeout 00:00:3000:00:30 要聚合的最大时间段。Maximum time period to aggregate.

达到两个限制中的第一个限制时,会聚合函数调用。Function invocations are aggregated when the first of the two limits are reached.

applicationInsightsapplicationInsights

此设置是日志记录的子项。This setting is a child of logging.

控制 Application Insights 中的采样功能Controls the sampling feature in Application Insights.

{
    "applicationInsights": {
        "samplingSettings": {
          "isEnabled": true,
          "maxTelemetryItemsPerSecond" : 5
        }
    }
}

备注

日志采样可能会导致一些执行不会显示在 Application Insights 监视器边栏选项卡中。Log sampling may cause some executions to not show up in the Application Insights monitor blade.

属性Property 默认Default 描述Description
isEnabledisEnabled true 启用或禁用采样。Enables or disables sampling.
maxTelemetryItemsPerSecondmaxTelemetryItemsPerSecond 55 开始采样所要达到的阈值。The threshold at which sampling begins.

CosmosDBcosmosDb

可在 Cosmos DB 触发器和绑定中查找配置设置。Configuration setting can be found in Cosmos DB triggers and bindings.

durableTaskdurableTask

可在 Durable Functions 的绑定中查找配置设置。Configuration setting can be found in bindings for Durable Functions.

eventHubeventHub

可在事件中心触发器和绑定中查找配置设置。Configuration settings can be found in Event Hub triggers and bindings.

扩展extensions

该属性返回一个对象,其中包含所有特定于绑定的设置,例如 httpeventHubProperty that returns an object that contains all of the binding-specific settings, such as http and eventHub.

函数functions

作业主机运行的函数列表。A list of functions that the job host runs. 空数组表示运行所有函数。An empty array means run all functions. 仅供在本地运行时使用。Intended for use only when running locally. 在 Azure 的函数应用中,应改为按照如何在 Azure Functions 中禁用函数中的步骤禁用特定函数,而不是使用此设置。In function apps in Azure, you should instead follow the steps in How to disable functions in Azure Functions to disable specific functions rather than using this setting.

{
    "functions": [ "QueueProcessor", "GitHubWebHook" ]
}

functionTimeoutfunctionTimeout

指示所有函数的超时持续时间。Indicates the timeout duration for all functions. 它遵循 timespan 字符串格式。It follows the timespan string format. 在无服务器消耗计划中,有效范围为 1 秒至 10 分钟,默认值为 5 分钟。In a serverless Consumption plan, the valid range is from 1 second to 10 minutes, and the default value is 5 minutes.
在专用 (应用服务) 计划中, 没有整体限制, 默认值取决于运行时版本:In a Dedicated (App Service) plan, there is no overall limit, and the default depends on the runtime version:

  • 版本 1.x: 默认值为null, 表示没有超时。Version 1.x: the default is null, which indicates no timeout.
  • 版本 2.x: 默认值为30分钟。Version 2.x: the default value is 30 minutes. 值为-1指示未绑定的执行。A value of -1 indicates unbounded execution.
{
    "functionTimeout": "00:05:00"
}

healthMonitorhealthMonitor

主机运行状况监视器的配置设置。Configuration settings for Host health monitor.

{
    "healthMonitor": {
        "enabled": true,
        "healthCheckInterval": "00:00:10",
        "healthCheckWindow": "00:02:00",
        "healthCheckThreshold": 6,
        "counterThreshold": 0.80
    }
}
属性Property 默认Default 描述Description
enabledenabled true 指定是否已启用该功能。Specifies whether the feature is enabled.
healthCheckIntervalhealthCheckInterval 10 秒10 seconds 定期后台运行状况检查之间的时间间隔。The time interval between the periodic background health checks.
healthCheckWindowhealthCheckWindow 2 分钟2 minutes healthCheckThreshold 设置结合使用的滑动时间窗口。A sliding time window used in conjunction with the healthCheckThreshold setting.
healthCheckThresholdhealthCheckThreshold 66 在启动主机回收之前,运行状况检查可以失败的最大次数。Maximum number of times the health check can fail before a host recycle is initiated.
counterThresholdcounterThreshold 0.800.80 性能计数器将被视为不正常的阈值。The threshold at which a performance counter will be considered unhealthy.

httphttp

可在 http 触发器和绑定中查找配置设置。Configuration settings can be found in http triggers and bindings.

{
    "http": {
        "routePrefix": "api",
        "maxOutstandingRequests": 200,
        "maxConcurrentRequests": 100,
        "dynamicThrottlesEnabled": true
    }
}
属性Property 默认Default 描述Description
routePrefixroutePrefix apiapi 应用到所有路由的路由前缀。The route prefix that applies to all routes. 使用空字符串可删除默认前缀。Use an empty string to remove the default prefix.
maxOutstandingRequestsmaxOutstandingRequests 200*200* 在任意给定时间搁置的未完成请求数上限。The maximum number of outstanding requests that are held at any given time. 此限制包括已排队但尚未开始执行的请求,以及正在执行的所有请求。This limit includes requests that are queued but have not started executing, as well as any in progress executions. 超出此限制的任何传入请求将被拒绝,并返回 429“太忙”响应。Any incoming requests over this limit are rejected with a 429 "Too Busy" response. 允许调用方使用基于时间的重试策略,还可帮助控制最大请求延迟。That allows callers to employ time-based retry strategies, and also helps you to control maximum request latencies. 此设置仅控制脚本宿主执行路径中发生的排队。This only controls queuing that occurs within the script host execution path. 其他队列(例如 ASP.NET 请求队列)仍有效,不受此设置的影响。Other queues such as the ASP.NET request queue will still be in effect and unaffected by this setting. *版本 1.x 的默认值是无限制的 (-1)。*The default for version 1.x is unbounded (-1). 消耗计划中版本 2.x 的默认值为 200。The default for version 2.x in a consumption plan is 200. 专用计划中版本 2.x 的默认值是无限制的 (-1)。The default for version 2.x in a dedicated plan is unbounded (-1).
maxConcurrentRequestsmaxConcurrentRequests 100*100* 要并行执行的 http 函数数目上限。The maximum number of http functions that will be executed in parallel. 这样,可以控制并发性,从而帮助管理资源利用率。This allows you to control concurrency, which can help manage resource utilization. 例如,某个 http 函数可能使用了大量系统资源(内存/CPU/插槽),从而在并发性过高时导致问题。For example, you might have an http function that uses a lot of system resources (memory/cpu/sockets) such that it causes issues when concurrency is too high. 或者,某个函数向第三方服务发出出站请求,则可能需要限制这些调用的速率。Or you might have a function that makes outbound requests to a third party service, and those calls need to be rate limited. 在这种情况下,应用限制可能有帮助。In these cases, applying a throttle here can help. *版本 1.x 的默认值是无限制的 (-1)。*The default for version 1.x is unbounded (-1). 消耗计划中版本 2.x 的默认值为 100。The default for version 2.x in a consumption plan is 100. 专用计划中版本 2.x 的默认值是无限制的 (-1)。The default for version 2.x in a dedicated plan is unbounded (-1).
dynamicThrottlesEnableddynamicThrottlesEnabled true*true* 启用时,将为此设置将导致请求处理管道,以定期检查系统性能计数器类似连接/线程/进程/内存/CPU 等,并通过内置的高阈值 (80%),如果有任何这些计数器请求拒绝与 429“太忙”响应,直至恢复到正常水平的计数器。When enabled, this setting causes the request processing pipeline to periodically check system performance counters like connections/threads/processes/memory/cpu/etc. and if any of those counters are over a built-in high threshold (80%), requests will be rejected with a 429 "Too Busy" response until the counter(s) return to normal levels. *版本 1.x 的默认值是 false。*The default for version 1.x is false. 消耗计划中版本 2.x 的默认值为 true。The default for version 2.x in a consumption plan is true. 专用计划中版本 2.x 的默认值为 false。The default for version 2.x in a dedicated plan is false.

logginglogging

控制函数应用(包括 Application Insights)的日志记录行为。Controls the logging behaviors of the function app, including Application Insights.

"logging": {
    "fileLoggingMode": "debugOnly",
    "logLevel": {
      "Function.MyFunction": "Information",
      "default": "None"
    },
    "console": {
        ...
    },
    "applicationInsights": {
        ...
    }
}
属性Property 默认Default 描述Description
fileLoggingModefileLoggingMode debugOnlydebugOnly 定义启用哪种级别的文件日志记录。Defines what level of file logging is enabled. 选项包括 neveralwaysdebugOnlyOptions are never, always, debugOnly.
logLevellogLevel 不适用n/a 一个对象,它定义了用于筛选应用中的函数的日志类别。Object that defines the log category filtering for functions in the app. 版本 2.x 遵循 ASP.NET Core 布局进行日志类别筛选。Version 2.x follows the ASP.NET Core layout for log category filtering. 这允许你筛选特定函数的日志记录。This lets you filter logging for specific functions. 有关详细信息,请参阅 ASP.NET Core 文档中的日志筛选For more information, see Log filtering in the ASP.NET Core documentation.
控制台console 不适用n/a console日志记录设置。The console logging setting.
applicationInsightsapplicationInsights 不适用n/a applicationInsights 设置。The applicationInsights setting.

控制台console

此设置是日志记录的子项。This setting is a child of logging. 它在未处于调试模式时控制控制台日志记录。It controls the console logging when not in debugging mode.

{
    "logging": {
    ...
        "console": {
          "isEnabled": "false"
        },
    ...
    }
}
属性Property 默认Default 描述Description
isEnabledisEnabled false 启用或禁用控制台日志记录。Enables or disables console logging.

queuesqueues

可在存储队列触发器和绑定中查找设置。Configuration settings can be found in Storage queue triggers and bindings.

SendGridsendGrid

可在 SendGrid 触发器和绑定中查找配置设置。Configuration setting can be found in SendGrid triggers and bindings.

serviceBusserviceBus

可在服务总线触发器和绑定中查找配置设置。Configuration setting can be found in Service Bus triggers and bindings.

singletonsingleton

单一实例锁行为的配置设置。Configuration settings for Singleton lock behavior. 有关详细信息,请参阅有关单一实例支持的 GitHub 问题For more information, see GitHub issue about singleton support.

{
    "singleton": {
      "lockPeriod": "00:00:15",
      "listenerLockPeriod": "00:01:00",
      "listenerLockRecoveryPollingInterval": "00:01:00",
      "lockAcquisitionTimeout": "00:01:00",
      "lockAcquisitionPollingInterval": "00:00:03"
    }
}
属性Property 默认Default 描述Description
lockPeriodlockPeriod 00:00:1500:00:15 占用函数级锁的时间段。The period that function level locks are taken for. 锁自动续订。The locks auto-renew.
listenerLockPeriodlistenerLockPeriod 00:01:0000:01:00 占用侦听器锁的时间段。The period that listener locks are taken for.
listenerLockRecoveryPollingIntervallistenerLockRecoveryPollingInterval 00:01:0000:01:00 在启动时无法获取侦听器锁的情况下,用于恢复侦听器锁的时间间隔。The time interval used for listener lock recovery if a listener lock couldn't be acquired on startup.
lockAcquisitionTimeoutlockAcquisitionTimeout 00:01:0000:01:00 运行时尝试获取锁的最长时间。The maximum amount of time the runtime will try to acquire a lock.
lockAcquisitionPollingIntervallockAcquisitionPollingInterval 不适用n/a 尝试获取锁的间隔时间。The interval between lock acquisition attempts.

versionversion

对于面向 v2 运行时的函数应用,版本字符串 "version": "2.0" 是必需的。The version string "version": "2.0" is required for a function app that targets the v2 runtime.

watchDirectorieswatchDirectories

应该监视其更改情况的一组共享代码目录A set of shared code directories that should be monitored for changes. 确保当这些目录中的代码发生更改时,函数会拾取这些更改。Ensures that when code in these directories is changed, the changes are picked up by your functions.

{
    "watchDirectories": [ "Shared" ]
}

managedDependencymanagedDependency

托管依赖项是当前仅支持基于 PowerShell 的函数的预览功能。Managed dependency is a preview feature that is currently only supported with PowerShell based functions. 它允许服务自动管理依赖项。It enables dependencies to be automatically managed by the service. 如果 enabled 属性设置为 true, 则将处理psd1文件。When the enabled property is set to true, the requirements.psd1 file will be processed. 发布任何次要版本时, 将更新依赖项。Dependencies will be updated when any minor versions are released.

{
    "managedDependency": {
        "enabled": true
    }
}

后续步骤Next steps