在 Azure 门户中的何处可以找到自定义指标
Where to find custom metrics in Azure Portal
我正在尝试将自定义指标发送到 Azure 门户中给定检测密钥的应用服务。作为后台服务的一部分,我在 .NET Core 中有以下代码 运行:
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}");
}
}
本示例代码来自here.
但是我不确定这些指标是否到达了 Azure 实例或在哪里寻找它们。我转到 Application Insights > MyInstance > Logs 并在其中找到一个名为 'customEvents' 的 table。但是我无法查询它。在“指标”选项卡上,我只获得默认指标命名空间,它显示 Azure 中可用的默认指标,但没有任何新的自定义指标。
在我的初创公司中,我确实有
services.AddApplicationInsightsTelemetry();
正如您所说,您正在使用后台工作者
- 安装包 nuget
Microsoft.ApplicationInsights.WorkerService
- 在你的启动中:
-
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
.....
services.AddApplicationInsightsTelemetryWorkerService();
});
简单的查找方法是转到应用洞察交易搜索并搜索事件名称
然后你点击事件,你会去下一个视图
这就是我检查您的事件是否实际记录的方式。
我也只是运行查询
customMetrics
| where name == "metric name"
customEvents
| where name == "Error.PageNotFound"
我认为问题是您使用这行代码创建了一个新的 telemetry client
:TelemetryClient telemetry = new TelemetryClient();
,但是新的 telemetry client
没有配置 InstrumentationKey
.然后 custom events
/ custom metrics
不使用 telemetry.TrackEvent
/ telemetry.TrackMetric
方法发送。
您应该像下面 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
}
}
}
这是我的 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();
});
}
}
这是我的 appsettings.json:
{
"ApplicationInsights": {
"InstrumentationKey": "your application insights InstrumentationKey"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
代码执行后,稍等几分钟,我可以在azure portal -> my application insights -> Logs中搜索custom metrics / custom events(对于自定义事件,查询customEvents
table):
我正在尝试将自定义指标发送到 Azure 门户中给定检测密钥的应用服务。作为后台服务的一部分,我在 .NET Core 中有以下代码 运行:
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}");
}
}
本示例代码来自here.
但是我不确定这些指标是否到达了 Azure 实例或在哪里寻找它们。我转到 Application Insights > MyInstance > Logs 并在其中找到一个名为 'customEvents' 的 table。但是我无法查询它。在“指标”选项卡上,我只获得默认指标命名空间,它显示 Azure 中可用的默认指标,但没有任何新的自定义指标。
在我的初创公司中,我确实有
services.AddApplicationInsightsTelemetry();
正如您所说,您正在使用后台工作者
- 安装包 nuget
Microsoft.ApplicationInsights.WorkerService
- 在你的启动中:
-
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { ..... services.AddApplicationInsightsTelemetryWorkerService(); });
简单的查找方法是转到应用洞察交易搜索并搜索事件名称
然后你点击事件,你会去下一个视图
这就是我检查您的事件是否实际记录的方式。
我也只是运行查询
customMetrics
| where name == "metric name"
customEvents
| where name == "Error.PageNotFound"
我认为问题是您使用这行代码创建了一个新的 telemetry client
:TelemetryClient telemetry = new TelemetryClient();
,但是新的 telemetry client
没有配置 InstrumentationKey
.然后 custom events
/ custom metrics
不使用 telemetry.TrackEvent
/ telemetry.TrackMetric
方法发送。
您应该像下面 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
}
}
}
这是我的 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();
});
}
}
这是我的 appsettings.json:
{
"ApplicationInsights": {
"InstrumentationKey": "your application insights InstrumentationKey"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
代码执行后,稍等几分钟,我可以在azure portal -> my application insights -> Logs中搜索custom metrics / custom events(对于自定义事件,查询customEvents
table):