[英]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正如你所说,你正在使用后台工作人员
Microsoft.ApplicationInsights.WorkerService
Microsoft.ApplicationInsights.WorkerService
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 client
: TelemetryClient 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.