AppMetrics如何设置应用程序名称?

时间:2019-09-18 10:11:47

标签: c# .net metrics

我在wcf应用程序(App.Metrics ver 3.1.0)中实现了App.Metrics。 当我查看上传数据的网址时,发现未填写 app enter image description here

试图找出这种现象的原因,我找到了手册: https://www.app-metrics.io/getting-started/fundamentals/tagging-organizing/

它说AssemblyName需要填写,但是我仔细检查了一下-csproj文件包含下一行:

<AssemblyName>MyWebService</AssemblyName>

如何在指标中填充此 app 属性?

2 个答案:

答案 0 :(得分:0)

startup.cs:

var metrics = MetricsProvider.Instance.Metrics;
SetMetricsAppTag(metrics, Assembly.GetExecutingAssembly().GetName().Name);   

private static void SetMetricsAppTag(IMetricsRoot metricsRoot, string appTagValue)
{
     if (!metricsRoot.Options.GlobalTags.ContainsKey("app"))
     {
           metricsRoot.Options.GlobalTags.Add("app", appTagValue);
     }
     else if (string.IsNullOrEmpty(metricsRoot.Options.GlobalTags["app"]) || metricsRoot.Options.GlobalTags["app"] == "unknown")
     {
           metricsRoot.Options.GlobalTags["app"] = appTagValue;
     }
}

答案 1 :(得分:0)

更好的方法是在启动时做-

var metrics = AppMetrics.CreateDefaultBuilder().
                Configuration.
                Configure(options => options.AddAppTag(appName: "nexus"))
               .Build();
            services.AddMetrics(metrics);
            services.AddMetricsTrackingMiddleware();
            services.AddMetricsEndpoints(opt =>
            {
                opt.MetricsTextEndpointOutputFormatter = new MetricsPrometheusTextOutputFormatter();
                opt.MetricsEndpointOutputFormatter = new MetricsPrometheusProtobufOutputFormatter();
                opt.EnvironmentInfoEndpointEnabled = false;
            });