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

ASP.NET Core MVC 8中IHttpMetricsTagsFeature获取为Null,如何启用?

启用ASP.NET Core 8中IHttpMetricsTagsFeature的操作步骤

1. 安装依赖包

首先确保项目安装了Microsoft.AspNetCore.Telemetry NuGet包,通过命令行执行:

dotnet add package Microsoft.AspNetCore.Telemetry

2. 注册HttpMetrics服务

在Program.cs的服务配置部分,添加HttpMetrics服务注册:

builder.Services.AddHttpMetrics();

3. 启用HttpMetrics中间件

在Program.cs的中间件管道配置中,添加UseHttpMetrics中间件,注意顺序要放在UseRouting之后、UseAuthorization之前:

app.UseRouting();
app.UseHttpMetrics();
app.UseAuthorization();

// 后续的路由映射,比如MVC控制器路由
app.MapControllers();

完成以上步骤后,在MVC动作中即可正常获取IHttpMetricsTagsFeature。你可以在动作中添加自定义标签,示例:

var tagsFeature = HttpContext.Features.Get<IHttpMetricsTagsFeature>();
if (tagsFeature != null)
{
    tagsFeature.Tags.Add("custom_tag", "custom_value");
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 13:25:54