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

配置BYOS关联Application Insights后,如何使用Azure Snapshot Debugger?

Azure Snapshot Debugger 后续配置与使用指南

一、无ApplicationInsights.config场景的基础设置

由于你使用NLog而非传统配置文件管理日志,需通过代码方式启用Snapshot Debugger:

1. 安装必要NuGet包

确保项目安装以下依赖:

  • Microsoft.ApplicationInsights.SnapshotCollector
  • 对应项目类型的Application Insights SDK包(比如ASP.NET Core用Microsoft.ApplicationInsights.AspNetCore)

2. 代码中启用Snapshot Debugger

ASP.NET Core 场景

在Program.cs中添加配置逻辑:

builder.Services.AddApplicationInsightsTelemetry();
builder.Services.AddSnapshotCollector(config =>
{
    // 自定义快照收集规则
    config.IsEnabled = true;
    config.SnapshotsPerTenantPerDay = 10; // 单日快照上限
    // 指定触发快照的异常类型(示例:空引用异常)
    config.ExceptionFilters.Add(new ExceptionFilter
    {
        ExceptionType = "System.NullReferenceException",
        MinimumSeverity = SeverityLevel.Error
    });
});

非ASP.NET Core 场景

在应用启动阶段初始化:

var telemetryConfig = new TelemetryConfiguration
{
    ConnectionString = "你的App Insights连接字符串"
};

var snapshotConfig = new SnapshotCollectorConfiguration
{
    IsEnabled = true,
    SnapshotsPerTenantPerDay = 10
};

SnapshotCollectorModule.Register(telemetryConfig, snapshotConfig);

二、异常追踪相关问题

1. 异常是否会自动追踪?

  • 未被处理的顶级异常:只要启用了Snapshot Debugger,会自动触发快照收集。
  • NLog手动记录的已处理异常:需确保NLog日志条目包含Exception属性,且日志级别为Error及以上,Snapshot Debugger才能识别并触发快照。

2. 关于TrackException

TrackException是Application Insights SDK原生的手动上报异常方法。你用NLog的话,只要正确配置了NLog的Application Insights目标,它会自动将日志中的异常转换为App Insights可识别的格式,无需手动调用TrackException。

三、利用快照进行调试

当异常触发快照后,按以下步骤调试:

  1. 登录Azure门户,打开你的Application Insights资源
  2. 在左侧菜单「调查」区域选择「快照调试器」
  3. 在快照列表中找到目标异常条目,点击「打开调试快照」
  4. 在调试界面中可操作:
    • 查看异常调用栈,点击栈帧定位对应代码行
    • 检查局部变量、参数的实时值
    • 查看线程状态
    • 添加临时只读断点(不会影响运行中的应用)

四、额外注意事项

  • 应用需运行在支持环境:Azure App Service、Azure VM、Azure Functions、本地服务器(需额外配置代理)等
  • 必须以Release模式运行应用,Debug模式不会触发快照收集
  • 确保BYOS存储账户权限配置正确,Application Insights拥有读写快照数据的权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 06:47:14