.NET Core Azure WebJob 的应用程序洞察
Application Insights for .NET Core Azure WebJob
当我尝试设置我的 .NET Core WebJob 以使用 Application Insights 时,我在启动时遇到以下异常:
System.InvalidOperationException:“尝试激活 'Microsoft.AspNetCore.Hosting.DefaultApplicationInsightsServiceConfigureOptions' 时无法解析类型 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' 的服务。”
我确定我忽略了一些东西,但我找不到它。我的 Main 方法就像在许多示例中一样,例如
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();
}
}
乍一看(和黑暗中的真实镜头),我会检查 context.Configuration
是否抛出异常,因为 APPINSIGHTS_INSTRUMENTATIONKEY 没有集合中不存在。丢失的键可能会抛出一个冒泡到您的 DI 容器的异常。
编辑:试试这个包,让我知道是否有效。它目前处于预发布阶段。 https://www.nuget.org/packages/Microsoft.Extensions.Logging.ApplicationInsights/
编辑:对 AddApplicationInsightsTelemetry 的调用使用 DefaultApplicationInsightsServiceConfigureOptions 依赖于 IHostingEnvironment。 WebJobsSDK which is causing your exception. The WebJobsSDK has it's on usage of Application Insights and you should be able to use it similarily to https://github.com/Azure/azure-webjobs-sdk/tree/dev/sample/SampleHost 示例
未使用 IHostingEnvironment
当我尝试设置我的 .NET Core WebJob 以使用 Application Insights 时,我在启动时遇到以下异常:
System.InvalidOperationException:“尝试激活 'Microsoft.AspNetCore.Hosting.DefaultApplicationInsightsServiceConfigureOptions' 时无法解析类型 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' 的服务。”
我确定我忽略了一些东西,但我找不到它。我的 Main 方法就像在许多示例中一样,例如
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();
}
}
乍一看(和黑暗中的真实镜头),我会检查 context.Configuration
是否抛出异常,因为 APPINSIGHTS_INSTRUMENTATIONKEY 没有集合中不存在。丢失的键可能会抛出一个冒泡到您的 DI 容器的异常。
编辑:试试这个包,让我知道是否有效。它目前处于预发布阶段。 https://www.nuget.org/packages/Microsoft.Extensions.Logging.ApplicationInsights/
编辑:对 AddApplicationInsightsTelemetry 的调用使用 DefaultApplicationInsightsServiceConfigureOptions 依赖于 IHostingEnvironment。 WebJobsSDK which is causing your exception. The WebJobsSDK has it's on usage of Application Insights and you should be able to use it similarily to https://github.com/Azure/azure-webjobs-sdk/tree/dev/sample/SampleHost 示例
未使用 IHostingEnvironment