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

CustomErrors配置与Azure App Insights日志的关联及代码逻辑疑问

关于CustomErrors配置与Application Insights异常追踪的矛盾解释

首先明确核心属性的对应关系:

  • ASP.NET中HttpContext.IsCustomErrorEnabled返回true时,对应CustomErrors配置为On,或是配置为RemoteOnly且当前请求为远程请求;仅当CustomErrors配置为Off时,该属性返回false。

接下来拆解矛盾本质:

  • 文档描述的是Application Insights默认HTTP模块的行为:当CustomErrors为Off时,异常不会被自定义错误页拦截,会直接暴露给HTTP模块,因此模块可自动捕获并上报异常。
  • 你贴出的代码是开发者自定义的异常追踪逻辑,这里的判断是为了补充默认模块的覆盖盲区:当CustomErrors开启(IsCustomErrorEnabled=true)时,默认HTTP模块无法捕获被自定义错误页掩盖的异常,因此需要手动调用TrackException主动上报这类被拦截的异常。

简言之,文档针对的是默认模块的自动捕获场景,代码则是开发者针对默认模块覆盖不到的场景做的手动补充,二者不存在真正的逻辑矛盾。

如果CustomErrors配置为Off,异常将可供HTTP模块收集。

对应的代码示例:

//If customError is Off, then AI HTTPModule will report the exception
if (filterContext.HttpContext.IsCustomErrorEnabled)
{   //or reuse instance (recommended!). see note above
    var ai = new TelemetryClient();
    ai.TrackException(filterContext.Exception);
}

注:代码中的注释存在表述歧义,正确逻辑应为:当CustomErrors开启(IsCustomErrorEnabled=true)时,默认模块无法捕获异常,需手动调用TrackException上报。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 08:06:29