To enable Application Insights logging in your WebJobs SDK host, use the AddApplicationInsightsWebJobs extension method within the ConfigureLogging callback of your host builder. This method allows you to configure ApplicationInsightsLoggerOptions, such as setting the InstrumentationKey.
Commonly used types in this package include:
ApplicationInsightsLoggingBuilderExtensions: Provides the extension methods for configuration.ApplicationInsightsLoggerProvider: The provider responsible for routing logs to Application Insights.ApplicationInsightsLoggerOptions: Configuration options for the logger.
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
class Program
{
public static async Task Main(string[] args)
{
var builder = Host.CreateDefaultBuilder(args)
.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorageQueues();
})
.ConfigureLogging((context, b) =>
{
// If this key exists in any config, use it to enable App Insights
string appInsightsKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
if (!string.IsNullOrEmpty(appInsightsKey))
{
b.AddApplicationInsightsWebJobs(o => o.InstrumentationKey = appInsightsKey);
}
});
using var host = builder.Build();
await host.RunAsync();
}
}