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

在Application Insights实时指标中无法查看日志条目如何解决?

如何在Live Metrics中实时查看Serilog跟踪日志

可以在Live Metrics模块中实时查看完整的跟踪日志,以下是针对你的.NET 7 + AKS环境的配置步骤:

1. 修正Serilog Sink的日志类型映射

你使用的Serilog.Sinks.ApplicationInsights v4.0.0默认会将日志转为Event类型,但Live Metrics默认监听的是Trace类型条目。需要显式指定TelemetryConverter为Traces类型:

代码配置方式:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Information() // 确保日志级别足够低,包含需要的条目
    .WriteTo.ApplicationInsights(
        new TelemetryConfiguration(Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING")),
        TelemetryConverter.Traces) // 关键:指定转换为Trace类型
    .CreateLogger();

配置文件(appsettings.json)方式:

"Serilog": {
  "MinimumLevel": {
    "Default": "Information"
  },
  "WriteTo": [
    {
      "Name": "ApplicationInsights",
      "Args": {
        "connectionString": "<你的Application Insights连接字符串>",
        "telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TracesTelemetryConverter, Serilog.Sinks.ApplicationInsights"
      }
    }
  ]
}

2. 确认Live Metrics面板的显示设置

  • 打开Azure门户的Live Metrics面板,点击顶部的Filters按钮,确保Traces选项处于勾选状态。
  • 切换到面板底部的Logs标签,检查日志级别过滤器(比如是否只筛选了Error级别的日志),调整为你需要的级别(如Information、Debug)。

3. 确保Application Insights Live Metrics服务启用

在Program.cs中显式启用Live Metrics服务(默认已启用,但显式配置更稳妥):

var builder = WebApplication.CreateBuilder(args);

// 添加Application Insights遥测服务
builder.Services.AddApplicationInsightsTelemetry();
// 启用Live Metrics功能
builder.Services.AddApplicationInsightsLiveMetrics();

// 其他配置...

var app = builder.Build();
// 其他中间件配置...

app.Run();

4. 验证AKS Pod的环境变量配置

确保AKS Pod的部署清单中正确配置了Application Insights的连接信息(推荐用连接字符串而非Instrumentation Key):

spec:
  containers:
  - name: your-api-container
    image: your-api-image:tag
    env:
    - name: APPLICATIONINSIGHTS_CONNECTION_STRING
      value: "<你的Application Insights连接字符串>"

5. 避免日志集成冲突

不要同时启用Serilog Sink和Application Insights原生日志提供者的重复配置,确保日志仅通过Serilog路由到Application Insights,防止日志分流导致Live Metrics无法捕获。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 07:35:22