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

如何在.NET Isolated Azure Function中通过appsettings.json配置Application Insights日志级别

问题

我想在Azure Functions项目里,像配置ASP.NET Core Web应用那样,通过appsettings.json来配置Application Insights的日志级别。默认情况下,Application Insights SDK会自动添加一个Warning级别的日志过滤器(如下图)。我知道仅设置Logging.LogLevel.Default不够,必须显式配置Logging.ApplicationInsights.LogLevel.Default,但即使这么做了,默认的Warning过滤器依然存在,只能在ConfigureLogging()里手动删除它。请问我哪里操作错了?

当前代码与配置

Program.cs

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication((context, builder) => { })
    .ConfigureAppConfiguration((context, builder) =>
    {
        var environment = context.Configuration.GetValue<string>("ENVIRONMENT", "development");

        builder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables();
    })
    ...
    .ConfigureLogging(builder =>
    {
        builder.Services.Configure<LoggerFilterOptions>(options =>
        {
            // 删除默认ApplicationInsights日志过滤器
            LoggerFilterRule defaultRule = options.Rules.FirstOrDefault(rule => rule.ProviderName
                == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
            if (defaultRule is not null)
            {
                options.Rules.Remove(defaultRule);
            }
        });
        builder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Information);
    })
    .Build();

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}

Application Insights默认Warning过滤器

解决方案

问题出在Azure Functions宿主的初始化顺序上:Application Insights的默认过滤器是在你调用ConfigureLogging之后,由Functions宿主自动添加的,导致你通过appsettings.json配置的规则被覆盖了。

正确做法

不需要手动删除默认规则,而是通过以下方式让配置文件的设置生效:

  • 调整日志配置的绑定方式
    在ConfigureLogging中直接利用已加载的配置来设置过滤器,确保配置优先于默认规则生效:

    .ConfigureLogging((context, builder) =>
    {
        // 直接从配置中读取Application Insights的日志级别
        var aiLogLevel = context.Configuration.GetValue<LogLevel>("Logging:ApplicationInsights:LogLevel:Default", LogLevel.Information);
        builder.AddFilter<ApplicationInsightsLoggerProvider>("", aiLogLevel);
        // 保留Functions的默认日志配置
        builder.AddAzureWebAppDiagnostics();
    })
    
  • 确保Application Insights服务正确初始化
    如果你没有显式添加Application Insights服务,Functions宿主会自动添加,但可能导致配置绑定延迟。可以显式添加服务并绑定配置:

    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        // 绑定Application Insights日志配置
        services.Configure<LoggerFilterOptions>(context.Configuration.GetSection("Logging"));
    })
    
  • 验证配置加载顺序
    确保ConfigureAppConfiguration在ConfigureLogging之前执行(你的代码里已经做到了),这样配置文件的内容能被日志系统读取到。

最终简化后的Program.cs示例

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureAppConfiguration((context, builder) =>
    {
        var environment = context.Configuration.GetValue<string>("ENVIRONMENT", "development");
        builder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
               .AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
               .AddEnvironmentVariables();
    })
    .ConfigureLogging((context, builder) =>
    {
        // 让日志系统自动读取Logging配置节点,包括ApplicationInsights的设置
        builder.AddConfiguration(context.Configuration.GetSection("Logging"));
        builder.AddAzureWebAppDiagnostics();
    })
    .Build();

host.Run();

这样修改后,appsettings.json里的Logging.ApplicationInsights.LogLevel.Default配置会自动生效,不需要手动删除默认过滤器。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 22:42:46