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

如何在APIM中禁止将404请求记录为Error日志

解决APIM中404请求被记录为Error日志的方法

我们在APIM上启用Application Insights日志记录后,开启“始终记录错误”并将详细程度设为“错误”,但发现404请求被记录为Error日志。即便应用已通过自定义ITelemetryInitializer将404标记为成功请求,仍可通过以下方法在APIM层面排除这类日志:

  • 通过APIM策略自定义日志逻辑
    在APIM的出站策略中添加条件判断,当响应状态码为404时,调整日志级别或跳过错误记录。示例策略代码:

    <outbound>
      <choose>
        <when condition="@(context.Response.StatusCode == 404)">
          <!-- 将404请求的日志级别设为Information,避免被识别为Error -->
          <log-to-application-insights logger-id="your-ai-logger-id" severity="Information">
            @($"Request: {context.Request.Method} {context.Request.Url} | Status Code: 404")
          </log-to-application-insights>
        </when>
        <otherwise>
          <!-- 正常记录Error级别日志 -->
          <log-to-application-insights logger-id="your-ai-logger-id" severity="Error">
            @($"Request: {context.Request.Method} {context.Request.Url} | Status Code: {context.Response.StatusCode}")
          </log-to-application-insights>
        </otherwise>
      </choose>
      <base />
    </outbound>
    

    需替换your-ai-logger-id为实际的Application Insights记录器ID。

  • 调整APIM的AI日志筛选规则
    在APIM的Application Insights配置中,找到日志筛选设置,添加规则not statusCode eq 404,直接排除404状态码的错误日志记录。

  • 在Application Insights中添加自定义遥测处理器
    创建自定义ITelemetryProcessor,针对APIM发送的遥测数据修正404请求的状态标记。示例C#代码:

    public class Exclude404ErrorsProcessor : ITelemetryProcessor
    {
        private readonly ITelemetryProcessor _nextProcessor;
    
        public Exclude404ErrorsProcessor(ITelemetryProcessor nextProcessor)
        {
            _nextProcessor = nextProcessor;
        }
    
        public void Process(ITelemetry telemetry)
        {
            if (telemetry is RequestTelemetry requestTelemetry && 
                requestTelemetry.ResponseCode == "404" &&
                requestTelemetry.Context.Cloud.RoleName == "your-apim-service-name")
            {
                // 将404请求标记为成功,从错误日志中移除
                requestTelemetry.Success = true;
            }
            _nextProcessor.Process(telemetry);
        }
    }
    

    编译部署该处理器后,APIM的404请求将不再在AI的“失败”页面中显示为错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 03:22:46