简体   繁体   English

在 Azure 门户中哪里可以找到自定义指标

[英]Where to find custom metrics in Azure Portal

I'm trying to send custom metrics to an App Service in Azure Portal given an instrumentation key.在给定检测密钥的情况下,我正在尝试将自定义指标发送到 Azure 门户中的应用服务。 I have the following code running in .NET Core as part of a Background service:作为后台服务的一部分,我在 .NET 核心中运行了以下代码:

public override async Task StartAsync(CancellationToken stoppingToken)
    {
        try
        {
            TelemetryClient telemetry = new TelemetryClient();
            telemetry.TrackEvent("new event");
            var sample = new MetricTelemetry();
            sample.Name = "metric name";
            sample.Value = 42.3;
            telemetry.TrackMetric(sample);
            telemetry.Flush();
        }
        catch (Exception e)
        {
            _logger.LogError($"{e.Message} {e.StackTrace}");
        }
    }

This sample code comes from: https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics此示例代码来自: https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics

However I'm not sure if these metrics are even reaching the Azure instance or where to look for them.但是我不确定这些指标是否甚至到达 Azure 实例或在哪里寻找它们。 I went to Application Insights > MyInstance > Logs and found a table there named 'customEvents'.我去了 Application Insights > MyInstance > Logs 并在那里找到了一个名为“customEvents”的表。 However I can't query over it.但是我无法查询它。 On the Metrics tabs I only get the default metrics namespace which shows the default metrics available in Azure, but not any new custom metrics.在 Metrics 选项卡上,我只获得了默认指标命名空间,它显示了 Azure 中可用的默认指标,但没有任何新的自定义指标。

In my startup I do have在我的创业公司中,我确实有

   services.AddApplicationInsightsTelemetry();

As you said you are using background worker正如你所说,你正在使用后台工作人员

  1. Install package nuget Microsoft.ApplicationInsights.WorkerService安装 package nuget Microsoft.ApplicationInsights.WorkerService
  2. in your startup:在您的启动中:
  •  public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureServices((hostContext, services) => {..... services.AddApplicationInsightsTelemetryWorkerService(); });

Simple way to find would be to go to app insight transaction search and search for event name简单的查找方法是 go 应用洞察交易搜索和搜索事件名称

在此处输入图像描述

Then you click on event and you will go to next view然后单击事件,您将 go 到下一个视图

在此处输入图像描述

Thats how I would check if you events are actually logged.这就是我检查您的事件是否实际记录的方式。

Also I just run queries我也只是运行查询

customMetrics
| where name == "metric name"

customEvents
| where name == "Error.PageNotFound"

在此处输入图像描述

I think the issue is that you create a new telemetry client by using this line of code: TelemetryClient telemetry = new TelemetryClient();我认为问题在于您使用以下代码行创建了一个新的telemetry clientTelemetryClient telemetry = new TelemetryClient(); , but the new telemetry client does not configure a InstrumentationKey . ,但新的telemetry client没有配置InstrumentationKey Then the custom events / custom metrics are not sent by using telemetry.TrackEvent / telemetry.TrackMetric methods.然后使用telemetry.TrackEvent / telemetry.TrackMetric方法不会发送custom events / custom metrics

You should change it like below in Worker.cs :您应该在Worker.cs中更改它,如下所示:

namespace WorkerService3
{
    public class Worker : BackgroundService
    {
        private readonly ILogger<Worker> _logger;

        //define a telemetry client here, and use it in your following code
        private TelemetryClient telemetry;

        public Worker(ILogger<Worker> logger, TelemetryClient tc)
        {
            _logger = logger;
            telemetry = tc;
        }

        public override async Task StartAsync(CancellationToken stoppingToken)
        {
            try
            {
                //do not create a another telemetry client, use the one defined in class-level.
                //TelemetryClient telemetry = new TelemetryClient();
                telemetry.TrackTrace("StartAsync: new message");
                telemetry.TrackEvent("StartAsync: new event");
                var sample = new MetricTelemetry();
                sample.Name = "StartAsync metric name";
                sample.Value = 11.55;
                telemetry.TrackMetric(sample);
                telemetry.Flush();
            }
            catch (Exception e)
            {
                _logger.LogError($"{e.Message} {e.StackTrace}");
            }

        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
           //other code
        }
    }
}

And this is my Program.cs :这是我的Program.cs

namespace WorkerService3
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<Worker>();
                    services.AddApplicationInsightsTelemetryWorkerService();
                });
    }
}

This is my appsettings.json:这是我的 appsettings.json:

{
  "ApplicationInsights": {
    "InstrumentationKey": "your application insights InstrumentationKey"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

After the code is executed, wait for a few minutes, I can search custom metrics / custom events in azure portal -> my application insights -> Logs(for custom events, query the customEvents table):代码执行后,等待几分钟,我可以在azure门户->我的应用洞察->日志中搜索自定义指标/自定义事件(自定义事件,查询customEvents表):

在此处输入图像描述

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

相关问题 在Azure门户中的何处查找日志信息 - Where to find log information in Azure portal 如何上传可在 Azure 管理门户上的诊断监视器中看到的 azure 上的自定义指标和指标数据 - How to upload custom metrics and metrics data on azure that can be seen on Azure Management Portal in Diagnostics Monitor .NET应用程序见解| 自定义指标未显示在portal.azure.com中的指标下 - Application insight .NET | Custom metrics don't show up in portal.azure.com under metrics 在新的Azure门户中哪里可以找到Azure SQL Sync? - Where do we find Azure SQL Sync in the new Azure Portal? 在完全Azure门户中的哪里可以找到ClearDB MySQL数据库? - Where to find ClearDB MySQL database in the Full Azure Portal? 在哪里可以找到 Azure 门户中的服务主体 GUID - Where can I find Service Principal GUID from Azure Portal 在Azure门户中的哪里可以找到web.config? - Where can I find the web.config in the Azure portal? 在 Azure 门户中哪里可以找到 Logic App 的受限事件? - Where to find throttled events of Logic App in Azure Portal? Azure门户度量提取供外部使用 - Azure Portal metrics extraction for external use Azure 门户上的 Cosmos DB 指标不正确 - Metrics for Cosmos DB on Azure portal are incorrect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM