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

.NET Core 6向App Insights写入日志异常:多项目配置问题排查

问题解答

多资源写入同一App Insights实例是否可行?

完全没问题。Azure App Insights支持多个不同类型的Azure资源(比如Function App、Web API、虚拟机等)将数据发送到同一个实例,这是官方认可的集中监控方案,能统一查看和分析所有关联服务的日志、性能指标与追踪数据。

Web API无法写入的常见配置问题排查

  • 确认Instrumentation Key/Connection String配置正确
    检查Web API的appsettings.json(或环境变量)中的App Insights配置项:

    • ASP.NET Core 6.0推荐使用Connection String而非旧版的InstrumentationKey,确保值和Function App中的完全一致,注意不要存在空格或拼写错误。
      示例配置:
    "ApplicationInsights": {
      "ConnectionString": "InstrumentationKey=xxx;IngestionEndpoint=xxx;LiveEndpoint=xxx"
    }
    
  • 确认已正确安装并配置App Insights NuGet包
    确保Web API项目已安装适配.NET 6.0的Microsoft.ApplicationInsights.AspNetCore包,并且在Program.cs中添加了App Insights服务:

    var builder = WebApplication.CreateBuilder(args);
    // 添加App Insights服务
    builder.Services.AddApplicationInsightsTelemetry();
    var app = builder.Build();
    // 其他中间件配置...
    app.Run();
    
  • 检查日志级别配置
    确认Web API的日志过滤规则没有阻止日志发送到App Insights。比如在appsettings.json中,要保证App Insights的日志级别设置合理:

    "Logging": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.AspNetCore": "Warning"
      },
      "ApplicationInsights": {
        "LogLevel": {
          "Default": "Information"
        }
      }
    }
    

    若设置了过高的日志级别(比如仅允许Error级别),Info级别的日志将不会被发送。

  • 检查Azure门户中的Web API应用配置
    即使本地配置正确,Azure门户中Web App的应用设置可能覆盖本地配置。进入Web API对应的App Service,查看配置>应用程序设置,确认APPINSIGHTS_CONNECTION_STRING或ApplicationInsights:InstrumentationKey的值与Function App一致。

  • 检查网络和防火墙设置
    如果Web API部署在虚拟网络中或有防火墙限制,需确保允许出站请求访问App Insights的 ingestion 端点(通常为https://dc.services.visualstudio.com/v2/track类地址)。可在App Insights实例的概述页面查看具体的ingestion端点地址,确认网络规则未拦截相关请求。

  • 查看Web API的本地日志或Azure诊断日志
    本地运行时查看控制台输出,检查是否有App Insights相关的错误信息;部署到Azure后,查看Web App的日志流或诊断日志,确认是否存在发送日志失败的报错(如认证错误、端点不可达等)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 13:50:40