Home > Enterprise >  I can't add Application Insight telemetry to my asp.net core console application
I can't add Application Insight telemetry to my asp.net core console application

Time:11-30

I have a console application on .NET 6. I try to log to Azure Application Insights.

Here is my configuration method:

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);
        });
}

But when I run the console application it gives the below error:

enter image description here

It perfectly works on Web API but I couldn't make it in Console App.

CodePudding user response:

Based on Peter's answer,

I changed the configure service method like below and it worked.

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>>();
        });
}

also, here is the offical documentation:

https://docs.microsoft.com/en-us/azure/azure-monitor/app/worker-service#net-corenet-framework-console-application

  • Related