【问题标题】:Application Insights for .NET Core Azure WebJob适用于 .NET Core Azure WebJob 的 Application Insights
【发布时间】:2019-07-05 06:04:21
【问题描述】:

当我尝试设置我的 .NET Core WebJob 以使用 Application Insights 时,我在启动时收到以下异常:

System.InvalidOperationException:“尝试激活“Microsoft.AspNetCore.Hosting.DefaultApplicationInsightsServiceConfigureOptions”时,无法解析“Microsoft.AspNetCore.Hosting.IHostingEnvironment”类型的服务。”

我确定我忽略了某些东西,但我找不到。我的 Main 方法就像在许多示例中一样,例如 here

   public static void Main(string[] args)
    {
        var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        Configuration.EnvironmentName = environment;
        Configuration.IsDevelopment = string.Equals(environment, "Development");

        var host = new HostBuilder()
            .UseEnvironment("Development")
            .ConfigureWebJobs(b =>
            {
                b.UseHostId("ecad61-62cf-47f4-93b4-6efcded6")
                .AddAzureStorageCoreServices()
                .AddAzureStorage()
                .AddTimers()
                .AddEventHubs();
            })
            .ConfigureServices(x => ConfigureServices(x))
            .ConfigureAppConfiguration(b =>
            {
                b.AddJsonFile("appsettings.json", false, false);
                b.AddJsonFile($"appsettings.{environment}.json", true);
                b.AddEnvironmentVariables();
                Configuration.Config = b.Build();
            })
            .ConfigureLogging((context, b) =>
            {
                b.AddConfiguration(Configuration.Config);
                b.SetMinimumLevel(LogLevel.Trace);
                b.AddConsole();
                b.AddDebug();
                //TODO fix applicationInsights
                string appInsightsKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
                if (!string.IsNullOrEmpty(appInsightsKey))
                {
                    b.AddApplicationInsights(o => o.InstrumentationKey = appInsightsKey);
                }
            })
            .Build();

        using (host)
        {
            host.Run();
        }
    }

【问题讨论】:

    标签: .net-core azure-webjobs


    【解决方案1】:

    乍一看(并且是在黑暗中真实拍摄),我会检查 context.Configuration 这是否会引发异常,因为集合中不存在 APPINSIGHTS_INSTRUMENTATIONKEY。缺少的键可能会引发一个异常,该异常会冒泡到您的 DI 容器中。

    编辑:尝试尝试使用此软件包,如果可行,请告诉我。它目前处于预发布状态。 https://www.nuget.org/packages/Microsoft.Extensions.Logging.ApplicationInsights/

    编辑:对 AddApplicationInsightsTelemetry 的调用使用 DefaultApplicationInsightsServiceConfigureOptions 取决于 IHostingEnvironment。 WebJobsSDK 没有使用 IHostingEnvironment,这会导致您的异常。 WebJobsSDK 有关于 Application Insights 的使用,你应该能够像 https://github.com/Azure/azure-webjobs-sdk/tree/dev/sample/SampleHost 示例一样使用它

    【讨论】:

    • 嗨!这条线实际上不太正确,因为我正在使用这个 Configuration.Config 对象。但无论如何,它奏效了,而且不是问题。我尝试创建自己的 IHostingEnvironment 对象,并将其添加到 ConfigureServices 方法中,但这也没有解决。
    • 您介意发布您的项目引用的 nuget 包吗?我正在尝试回购这个问题,但缺少一些依赖项,我想确保我使用的是你正在使用的东西。
    • "Microsoft.ApplicationInsights.AspNetCore" 版本="2.4.1" "Microsoft.AspNetCore.Http" 版本="2.2.0" "Microsoft.AspNetCore.Http.Abstractions" 版本="2.2。 0""Microsoft.Azure.WebJobs"版本="3.0.4"Microsoft.Azure.WebJobs.Extensions"版本="3.0.1""Microsoft.Azure.WebJobs.Extensions.EventHubs"版本="3.0.2"" Microsoft.Azure.WebJobs.Extensions.Storage" 版本="3.0.3" "Microsoft.Azure.WebJobs.Host.Storage" 版本="3.0.4" "Microsoft.Azure.WebJobs.Logging.ApplicationInsights" 版本="3.0 .4" "Microsoft.Azure.WebJobs.ServiceBus" 版本="2.3.0"
    • "Microsoft.Extensions.DependencyInjection" Version="2.2.0" "Microsoft.Extensions.Logging.Console" Version="2.2.0" "Microsoft.Extensions.Logging.Debug" Version=" 2.2.0" "Microsoft.Extensions.Options.ConfigurationExtensions" 版本="2.2.0"
    • 我的 Main() 没有遇到任何明显的问题。 ConfigureServices(IServicesCollection x) 是做什么的?
    猜你喜欢
    • 2018-01-05
    • 2022-12-02
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 2019-12-21
    相关资源
    最近更新 更多