问题
How to add application insights telemetry (Application Insights) to azure webjob ?
回答1:
With recently released WebJob SDK 3.0, you can add ApplicationInsights in the ConfigureLogging method
public static async Task Main(string[] args)
{
var builder = new HostBuilder()
.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices().AddAzureStorage();
})
.ConfigureAppConfiguration(b =>
{
// Adding command line as a configuration source
b.AddCommandLine(args);
})
.ConfigureLogging((context, b) =>
{
b.SetMinimumLevel(LogLevel.Debug);
b.AddConsole();
// If this key exists in any config, use it to enable App Insights
string appInsightsKey = context.Configuration["ApplicationInsights:InstrumentationKey"];
if (!string.IsNullOrEmpty(appInsightsKey))
{
b.AddApplicationInsights(o => o.InstrumentationKey = appInsightsKey);
}
});
var host = builder.Build();
using (host)
{
await host.RunAsync();
}
}
回答2:
You can add AI to Web Jobs during the development time as a Nuget Package.
AI .NET Core Nuget is here. Package name is a bit misleading (Microsoft.ApplicationInsights.AspNetCore
) but it's supposed to work with all .Net core apps.
AI .NET Core GitHub page is here (with some customization options explained in Wiki).
Getting started guide is on GitHub and docs.microsoft.com as well. It's a bit lengthy guide, so I hope links are OK (although not in full accordance with SO guidelines) and I won't need to post it as part of the reply.
来源:https://stackoverflow.com/questions/52424215/application-insights-to-azure-webjob-net-core-2-0