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

Azure静态Web应用无法查看Warning级别以下的Function日志如何解决?

问题描述

我创建了一个Azure Static Web App,在Application Insights(Monitor)中可以看到API函数的请求执行记录,但无法查看Warning级别以下的日志消息。我在Azure Function中按以下方式记录日志:

_logger.LogTrace($">>> Log Trace");
_logger.LogDebug($">>> Log Debug");
_logger.LogInformation($">>> Log Information");
_logger.LogWarning($">>> Log Warning");
_logger.LogError($">>> Log Error");
_logger.LogCritical($">>> Log Critical");

Console.WriteLine($">>> Console WriteLine");

目前仅能看到Warning及以上级别的日志,缺少Trace、Debug、Information级别的日志。

现有配置

.csproj文件

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    </PropertyGroup>
    <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.3">
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
    </ItemGroup>
    <ItemGroup>
    <ProjectReference Include="..\SoutienScolaireV2.Shared\SoutienScolaireV2.Shared.csproj" />
    </ItemGroup>
    <ItemGroup>
    <None Update="Data\database.db">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="host.json">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
    </ItemGroup>
    <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
    </ItemGroup>
    <ItemGroup>
    <Folder Include="Data\" />
    </ItemGroup>
</Project>

host.json文件

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "logLevel": {
        "default": "Information",
        "Function": "Information"
      },
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request",
        "logLevel": {
          "default": "Trace"
        }
      },
      "enableLiveMetricsFilters": true
    },
    "logLevel": {
      "default": "Information",
      "Function": "Information"
    }
  }
}

请问需要补充什么配置才能查看所有日志消息?


解决方案

要让Application Insights捕获所有日志级别(包括Trace、Debug),需调整以下配置:

1. 修改host.json的全局日志级别

将logging下的logLevel和applicationInsights.logLevel的default、Function级别统一改为Trace,确保日志管道允许最低级别日志输出:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "logLevel": {
        "default": "Trace",
        "Function": "Trace"
      },
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request",
        "logLevel": {
          "default": "Trace"
        }
      },
      "enableLiveMetricsFilters": true
    },
    "logLevel": {
      "default": "Trace",
      "Function": "Trace"
    }
  }
}

2. 调整采样设置(关键)

采样功能会过滤部分低级别日志以降低成本,若需要完整日志记录,可暂时禁用采样:

"samplingSettings": {
  "isEnabled": false,
  "excludedTypes": "Request",
  "logLevel": {
    "default": "Trace"
  }
}

注意:生产环境禁用采样会增加日志量和成本,需根据实际需求权衡。

3. 配置Azure门户端的诊断设置

登录Azure门户,找到对应Application Insights资源:

  • 进入配置 > 诊断设置
  • 确保选中的诊断设置中,Function App Logs的日志级别设为Verbose(包含所有级别日志)
  • 保存设置

4. 捕获Console.WriteLine输出

Console.WriteLine默认不会发送到Application Insights,需在Program.cs中添加配置,将控制台输出桥接到ILogger:

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureLogging(logging =>
    {
        logging.AddConsole();
        logging.Services.Configure<LoggerFilterOptions>(options =>
        {
            options.MinLevel = LogLevel.Trace;
        });
    })
    .Build();

host.Run();

5. 验证NuGet包兼容性

确保Microsoft.Azure.Functions.Worker.ApplicationInsights版本(当前1.1.0)支持上述配置,若存在问题可升级至最新稳定版。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 08:24:52