Azure App Service中ASP.NET MVC应用/admin/host/ping 404日志处理咨询
处理Azure App Service自动扩缩容导致的/admin/host/ping冗余404日志方案
方法1:在ASP.NET MVC中添加匹配路由,返回200状态码
直接在应用内为该健康检查路径注册路由,返回成功响应,从源头避免404日志生成:
- 在
RouteConfig.cs中添加路由规则:routes.MapRoute( name: "PingHealthCheck", url: "admin/host/ping", defaults: new { controller = "Health", action = "Ping" } ); - 创建
HealthController并实现Ping方法:public class HealthController : Controller { public HttpResponseMessage Ping() { return new HttpResponseMessage(HttpStatusCode.OK); } }
该方案既满足自动扩缩容的健康检测逻辑,又不会产生冗余404日志。
方法2:在Application Insights中过滤该路径日志
若不想修改应用代码,可通过Application Insights的配置过滤或排除该路径的日志:
- 实时查询过滤:查看日志时添加排除条件,示例Kusto查询:
requests | where url !contains "/admin/host/ping" and resultCode == "404" - 数据收集排除规则:在Application Insights的「数据收集-采样」设置中,添加自定义排除规则,匹配URL包含
/admin/host/ping的请求,后续该路径日志将不再被收集。
方法3:修改Azure App Service健康检查路径
调整自动扩缩容的健康检测端点为应用已存在的健康路径:
- 进入App Service的「扩缩容」设置页面
- 在「健康检查」区域,将检测路径修改为应用内已实现的健康端点(如
/health) - 确保新端点返回200状态码,自动扩缩容将使用新路径进行检测,不再生成目标冗余请求。
方法4:通过Web.config配置URL重写拦截请求
借助IIS URL重写模块直接处理该请求,无需修改应用代码:
- 在
Web.config的<system.webServer>节点下添加重写规则:<rewrite> <rules> <rule name="HandlePingHealthCheck" stopProcessing="true"> <match url="^admin/host/ping$" /> <action type="CustomResponse" statusCode="200" statusReason="OK" statusDescription="OK" /> </rule> </rules> </rewrite>
该规则会拦截所有指向/admin/host/ping的请求,直接返回200响应,避免IIS生成404日志。
内容的提问来源于stack exchange,提问作者Shifath
相关产品推荐
相关产品推荐

