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

Azure App Insight排除Hangfire日志失败:telemetry.Name无Hangfire标识

在Azure App Insights中排除Hangfire相关SQL依赖日志

我需要在Azure App Insights中排除Hangfire产生的SQL依赖日志,参考相关内容编写了以下过滤代码:

if (telemetry.Type == "SQL"
    && telemetry.Name.ToLower().Contains("hangfire")
    && telemetry.Success.GetValueOrDefault(false))
{
    return;
}

但实际排查发现,telemetry.Name的值为xx.xxx.xx.xx,xxx | myservices,并不包含"hangfire"关键字,导致过滤规则失效,Hangfire相关日志仍会被正常处理(执行next.Process(item);)。以下是未被过滤的日志示例:

10/25/2022, 10:57:10 PM

10/25/2022, 10:52:10 PM

10/25/2022, 10:52:10 PM

10/25/2022, 10:52:10 PM

10/25/2022, 10:52:10 PM

10/25/2022, 10:52:10 PM

10/25/2022, 10:52:10 PM

10/25/2022, 10:52:10 PM

10/25/2022, 10:52:10 PM

10/25/2022, 10:52:10 PM

10/25/2022, 10:47:10 PM

解决思路与修改后的代码

问题出在telemetry.Name仅存储数据库服务器和实例信息,不包含具体SQL操作内容。要过滤Hangfire相关的SQL日志,需要通过SQL命令文本或自定义属性来识别:

  1. 通过SQL命令文本判断
    将telemetry强转为SqlDependencyTelemetry,获取具体的SQL命令文本,检查是否包含Hangfire相关表或操作:
var sqlTelemetry = telemetry as SqlDependencyTelemetry;
if (sqlTelemetry != null
    && !string.IsNullOrEmpty(sqlTelemetry.CommandText)
    && sqlTelemetry.CommandText.ToLower().Contains("hangfire")
    && telemetry.Success.GetValueOrDefault(false))
{
    return; // 跳过该日志的上报
}
  1. 通过自定义属性判断
    如果Hangfire在生成SQL请求时添加了自定义属性(比如HangfireJobId),可以直接检查Properties集合:
if (telemetry.Type == "SQL"
    && telemetry.Properties.ContainsKey("HangfireJobId")
    && telemetry.Success.GetValueOrDefault(false))
{
    return; // 跳过该日志的上报
}

验证方法

修改代码后,可在本地调试时查看SqlDependencyTelemetry的CommandText或Properties内容,确认Hangfire相关操作的特征,再调整过滤条件确保准确匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 21:25:24