Application Insights 检测 .NET ASP (Framework 4.6.2) 应用程序中的服务总线依赖性。它在 Azure 门户的应用程序映射中将服务总线显示为依赖项。但它不适用于 Windows 服务(控制台)。我对服务使用与 API 相同的 Application Insights 配置,但它不起作用。其他依赖项可以完美检测到。 如何修复它?或者存在什么诊断方法?
ApplicationInsights.config:
<Add Type="Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule, Microsoft.AI.DependencyCollector">
<ExcludeComponentCorrelationHttpHeadersOnDomains>
<Add>core.windows.net</Add>
<Add>core.chinacloudapi.cn</Add>
<Add>core.cloudapi.de</Add>
<Add>core.usgovcloudapi.net</Add>
</ExcludeComponentCorrelationHttpHeadersOnDomains>
<IncludeDiagnosticSourceActivities>
<Add>Microsoft.Azure.EventHubs</Add>
<Add>Microsoft.Azure.ServiceBus</Add>
</IncludeDiagnosticSourceActivities>
</Add>
我们没有像在控制台(.NET Core 应用程序)中那样直接选择从连接服务配置 Application Insights。
我已参考此 MSDoc 在 .NET Framework 控制台应用程序中手动配置 Application Insights。
当您安装
Microsoft.ApplicationInsights.WorkerService
软件包时,ApplicationInsights.config
将自动创建所需的软件包。
在
ApplicationInsights.config
文件中添加仪器或连接字符串。更改以下属性。
创建服务总线命名空间,复制连接字符串并创建队列。
我可以查看服务总线依赖项,检查以下代码
我的
Program.cs
文件:
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.ServiceBus;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace AppInsightsConsole
{
class Program
{
private static IQueueClient queClient;
static async Task Main(string[] args)
{
IServiceCollection services = new ServiceCollection();
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
telemetryConfiguration.ConnectionString = TelemetryConfiguration.Active.ConnectionString;
services.AddLogging(loggingBuilder =>
loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("Category", LogLevel.Information));
services.AddSingleton(telemetryConfiguration);
services.AddSingleton<TelemetryClient>();
//services.AddApplicationInsightsTelemetryWorkerService((ApplicationInsightsServiceOptions options) =>
//options.ConnectionString = "InstrumentationKey=****");
IServiceProvider serviceProvider = services.BuildServiceProvider();
ILogger<Program> logger = serviceProvider.GetRequiredService<ILogger<Program>>();
var tc = serviceProvider.GetRequiredService<TelemetryClient>();
var httpClient = new HttpClient();
var serviceBusConnectionString = "Endpoint=sb://sep27sb.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=nSztLgQP4bY0t4OIFXXKUfP9tCsV90aT8+ASbPevYos=";
var queueName = "Sample";
queClient = new QueueClient(serviceBusConnectionString, queueName);
while (true)
{
using (var op = tc.StartOperation<DependencyTelemetry>("ServiceBus Sample"))
{
var mb = "Hello.. Service Bus";
var msg = new Message(System.Text.Encoding.UTF8.GetBytes(mb));
await queClient.SendAsync(msg);
logger.LogInformation($"Message sent : {mb} to Service Bus");
tc.TrackEvent("Message from Service Bus");
}
await Task.Delay(1000);
}
await queClient.CloseAsync();
tc.Flush();
await Task.Delay(5000);
}
}
}
应用图:
交易搜索中的依赖关系: