gpt4 book ai didi

c# - 依赖关系未显示在应用程序 map 中?

转载 作者:行者123 更新时间:2023-12-03 06:23:53 25 4
gpt4 key购买 nike

我有一个用 C# 和 .net 6 编写的控制台应用程序,它使用 Application Insights。

一些依赖项会自动发现并显示在应用程序图中。该应用程序还使用 MQTTnet 库将消息发送到 MQTT 代理。由于这些调用不会自动显示在应用程序映射中,因此我添加了对 TrackDependency 的调用以手动添加它,如下所示:

    var startTime = DateTime.UtcNow;
var timer = System.Diagnostics.Stopwatch.StartNew();
bool success = false;
try
{
await _mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
success = true;
}
finally
{
timer.Stop();
ApplicationTelemetry.TelemetryClient?.TrackDependency("EventBusTopic", "PublishAsync", null, startTime, timer.Elapsed, success);
}

我可以看到,通过在事务搜索中搜索“依赖项”,数据已提取到 Application Insights 中:

enter image description here

但是,依赖关系不会显示在应用程序映射中。

这是初始化 Application Insights 的代码:

    IServiceCollection services = new ServiceCollection();
ApplicationInsightsServiceOptions options = new ApplicationInsightsServiceOptions();
options.ConnectionString = configuration["ApplicationInsights:ConnectionString"];
services.AddApplicationInsightsTelemetryWorkerService(options);

IServiceProvider serviceProvider = services.BuildServiceProvider();

ApplicationTelemetry.TelemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();

可能是什么原因?

最佳答案

可能是因为它没有操作 ID。您应该开始这样的操作:

using (var op = ApplicationTelemetry.TelemetryClient.StartOperation<DependencyTelemetry>("PublishAsync"))
{
op.Telemetry.Type = "EventBusTopic";
op.Telemetry.Success = false;

await _mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
op.Telemetry.Success = true;
}

它将通过这种方式自动跟踪开始时间和持续时间,并获取操作 ID。如果已经有一个待处理的操作,它将作为子操作附加到该父操作。

关于c# - 依赖关系未显示在应用程序 map 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75693569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com