我无法将 Application Insight 遥测添加到我的 asp.net 核心控制台应用程序
I can't add Application Insight telemetry to my asp.net core console application
我在 .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 但我无法在控制台应用程序中实现它。
根据彼得的回答,
我像下面这样更改了配置服务方法,它起作用了。
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>>();
});
}
此外,这是官方文档:
我在 .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 但我无法在控制台应用程序中实现它。
根据彼得的回答,
我像下面这样更改了配置服务方法,它起作用了。
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>>();
});
}
此外,这是官方文档: