我无法将 Application Insight 遥测添加到我的 asp.net 核心控制台应用程序
Posted
技术标签:
【中文标题】我无法将 Application Insight 遥测添加到我的 asp.net 核心控制台应用程序【英文标题】:I can't add Application Insight telemetry to my asp.net core console application 【发布时间】:2022-01-05 04:05:50 【问题描述】:我在 .NET 6 上有一个控制台应用程序。 我尝试登录到 Azure Application Insights。
这是我的配置方法:
private static IHostBuilder CreateHostBuilder(string[] args)
return Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
services.AddTransient<Program>();
HttpManager manager = new HttpManager(Configuration);
SqlManager sqlManager = new SqlManager(Configuration);
EngineFunctions engineFunctions = new EngineFunctions();
EngineManager engineManager = new EngineManager(sqlManager, engineFunctions);
WorkOrderFunctions workOrderFunctions = new WorkOrderFunctions();
ServiceBusManager serviceBusManager = new ServiceBusManager(Configuration);
WorkOrderService wservice = new WorkOrderService(manager, sqlManager, workOrderFunctions, engineManager, serviceBusManager);
services.AddSingleton(wservice);
string instrumentationKey = Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
services.AddApplicationInsightsTelemetry(instrumentationKey);
);
但是当我运行控制台应用程序时,它给出了以下错误:
它在 Web API 上完美运行,但我无法在控制台应用程序中实现。
【问题讨论】:
您没有使用正确的软件包。试试this 谢谢,彼得。有效。如果答案是正确的,请投票给它。 【参考方案1】:根据彼得的回答,
我改变了配置服务的方法,如下所示。
private static IHostBuilder CreateHostBuilder(string[] args)
return Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
services.AddTransient<Program>();
HttpManager manager = new HttpManager(Configuration);
SqlManager sqlManager = new SqlManager(Configuration);
EngineFunctions engineFunctions = new EngineFunctions();
EngineManager engineManager = new EngineManager(sqlManager, engineFunctions);
WorkOrderFunctions workOrderFunctions = new WorkOrderFunctions();
ServiceBusManager serviceBusManager = new ServiceBusManager(Configuration);
WorkOrderService wservice = new WorkOrderService(manager, sqlManager, workOrderFunctions, engineManager, serviceBusManager);
services.AddSingleton(wservice);
services.AddLogging(loggingBuilder => loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("Category", LogLevel.Information));
services.AddApplicationInsightsTelemetryWorkerService(Configuration["ApplicationInsights:InstrumentationKey"]);
// Build ServiceProvider.
IServiceProvider serviceProvider = services.BuildServiceProvider();
// Obtain logger instance from DI.
_logger = serviceProvider.GetRequiredService<ILogger<Program>>();
);
另外,这里是官方文档:
https://docs.microsoft.com/en-us/azure/azure-monitor/app/worker-service#net-corenet-framework-console-application
【讨论】:
以上是关于我无法将 Application Insight 遥测添加到我的 asp.net 核心控制台应用程序的主要内容,如果未能解决你的问题,请参考以下文章
Azure Application Insight工作项目授权错误
最小 API .net 核心中的 Application Insight 日志记录
能否在实时应用程序中禁用或启用 Application Insight
在 Application Insight 和 Postman 中获取时间差异
我们如何在 Service Fabric Actor Reliable 服务中添加 Application Insight