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

如何在Azure Functions隔离模式下用Serilog有效禁用内置日志

针对Azure Functions v4隔离进程的日志过滤方案

1. 精准定位目标日志所属类别

你要屏蔽的日志分属以下官方日志类别,需精准覆盖:

  • Initiating background SyncTriggers operation:Microsoft.Azure.WebJobs.Host.Triggers.SyncTrigger
  • Request successfully matched the route with name '' and template 'admin/warmup':Microsoft.Azure.Functions.Worker.Http.RouteMatching
  • HttpMessageHandler expired/cleanup:Microsoft.Extensions.Http.DefaultHttpClientFactory
  • Sending invocation id:Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor
  • 若需屏蔽Executing/Executed日志:Microsoft.Azure.WebJobs.Host.Executors

2. 结合host.json与Serilog的双重配置

隔离进程中,host.json控制Functions Worker的原生日志输出,Serilog管理自定义日志管道,两者需配合生效:

步骤1:配置host.json

在logging.logLevel中添加对应类别的级别为None,阻止Worker输出这些日志:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    },
    "logLevel": {
      "Microsoft.Azure.WebJobs.Host.Triggers.SyncTrigger": "None",
      "Microsoft.Azure.Functions.Worker.Http.RouteMatching": "None",
      "Microsoft.Extensions.Http.DefaultHttpClientFactory": "None",
      "Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor": "None",
      // 如需屏蔽Executing/Executed日志,取消下方注释
      // "Microsoft.Azure.WebJobs.Host.Executors": "None"
    }
  }
}

步骤2:在Serilog中覆盖对应类别级别

在Program.cs的UseSerilog配置里,通过设置最高级别(Fatal)过滤目标日志,Serilog无None级别,用Fatal替代可实现相同效果:

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .UseSerilog((context, config) =>
    {
        config.ReadFrom.Configuration(context.Configuration)
              .MinimumLevel.Override("Microsoft.Azure.WebJobs.Host.Triggers.SyncTrigger", Serilog.Events.LogEventLevel.Fatal)
              .MinimumLevel.Override("Microsoft.Azure.Functions.Worker.Http.RouteMatching", Serilog.Events.LogEventLevel.Fatal)
              .MinimumLevel.Override("Microsoft.Extensions.Http.DefaultHttpClientFactory", Serilog.Events.LogEventLevel.Fatal)
              .MinimumLevel.Override("Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor", Serilog.Events.LogEventLevel.Fatal)
              // 如需屏蔽Executing/Executed日志,取消下方注释
              // .MinimumLevel.Override("Microsoft.Azure.WebJobs.Host.Executors", Serilog.Events.LogEventLevel.Fatal);
    })
    .Build();

host.Run();

3. 验证配置有效性

部署后查看函数日志流,确认目标日志不再输出即可。

关键注意事项

  • 隔离进程日志体系与In-process不同,Microsoft.AspNetCore类别的覆盖对当前场景无效,需针对Functions Worker专属类别配置
  • 必须同时配置host.json和Serilog,两者分别控制原生日志输出与自定义管道处理,缺一不可
  • 确保host.json位于项目根目录,部署时被正确打包至函数应用

内容的提问来源于stack exchange,提问作者evilmandarine

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 03:25:19