【问题标题】:I can't add Application Insight telemetry to my asp.net core console application我无法将 Application Insight 遥测添加到我的 asp.net 核心控制台应用程序
【发布时间】: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
  • 谢谢,彼得。有效。如果答案是正确的,请投票给它。

标签: asp.net-core console-application azure-application-insights azure-webjobs .net-6.0


【解决方案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

【讨论】:

    猜你喜欢
    • 2018-05-14
    • 2019-01-21
    • 2018-08-14
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    相关资源
    最近更新 更多