简体   繁体   English

使用 Telemetry Client 的 App Insights 中未显示自定义指标

[英]Custom Metrics not shown in App Insights with Telemetry Client

My application is an ASP.NET Core 6 application.我的应用程序是 ASP.NET Core 6 应用程序。 I am using Telemetry client of Application Insights and calling GetMetric to log custom metrics in the Azure Portal.我正在使用 Application Insights 的遥测客户端并调用GetMetric以在 Azure 门户中记录自定义指标。

I am using Microsoft.ApplicationInsights.AspNetCore version 2.21.0我正在使用Microsoft.ApplicationInsights.AspNetCore版本 2.21.0

The Telemetry Client is injected using a constructor:使用构造函数注入遥测客户端:

public RaftService (ILogger<RaftService> logger,
                    IHttpClientFactory clientFactory,
                    IOptions<RaftOptions> raftOptions,
                    IOrderNoteService orderNoteService,
                    TelemetryClient telemetryClient)
{
    _logger = logger;
    _clientFactory = clientFactory;
    _options = raftOptions;
    _orderNoteService = orderNoteService;
    _telemetryClient = telemetryClient;
}

I call the GetMetric and TrackValue methods as follows:我调用GetMetricTrackValue方法如下:

var _paymentAPIStatusCodeMetric = _telemetryClient.GetMetric("PaymentAPIStatusCode");
_paymentAPIStatusCodeMetric.TrackValue(Convert.ToInt32(response.StatusCode));

I am specifying the connection string in my appsettings.json file like this:我在appsettings.json文件中指定连接字符串,如下所示:

在此处输入图像描述

When I run the application locally in VS 2022, I can see the metrics getting logged.当我在 VS 2022 中本地运行应用程序时,我可以看到记录的指标。 My TelemetryClient object looks as follows.我的TelemetryClient object 如下所示。 I see the instrumentation key is assigned and the connection string is populated.:我看到检测密钥已分配并且连接字符串已填充。:

在此处输入图像描述

When I deploy my application as an App Service, custom metrics do not get logged.当我将我的应用程序部署为应用程序服务时,不会记录自定义指标。 My TelemetryClient object looks as follows - the instrumentation key is not getting populated in the TelemetryConfiguration for some reason and the DisableTelemetry is showing as true.我的TelemetryClient object 如下所示 - 由于某种原因,检测密钥未填充到TelemetryConfiguration中,并且DisableTelemetry显示为 true。

在此处输入图像描述

Any idea why the Instrumentation Key = "" and DisableTelemetry = true when running the application as an App Service in Azure?知道为什么在 Azure 中将应用程序作为应用程序服务运行时Instrumentation Key = ""DisableTelemetry = true吗?

I am guessing, since the instrumentation Key is not set, custom metrics are not getting logged in Azure Portal.我猜,由于未设置检测密钥,自定义指标未记录在 Azure 门户中。

Any help is much appreciated.任何帮助深表感谢。

Please change your code like below.请像下面这样更改您的代码。

public class RaftService: IDisposable
{
    private readonly TelemetryClient _tc;
    private readonly Stopwatch _sw = new Stopwatch();
    private readonly string _operationName;
    
    public RaftService(TelemetryClient tc, string operationName,ILogger<RaftService> logger,
                IHttpClientFactory clientFactory,
                IOptions<RaftOptions> raftOptions,
                IOrderNoteService orderNoteService)
    {
        _tc = tc;
        _operationName = operationName;
        _logger = logger;
        _clientFactory = clientFactory;
        _options = raftOptions;
        _orderNoteService = orderNoteService;
        _sw.Start();
    }
    
    public void Dispose()
    {
        _sw.Stop();
        _tc.GetMetric(_operationName).TrackValue(_sw.ElapsedMilliseconds);
    }
}

Then you can call it.然后就可以调用了。

using(new RaftService(tc, "metricName"))
{
    // 
}

Finally, you can check the logs.最后,您可以查看日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM