Search code examples
dependency-injectionazure-functionsazure-application-insightsazure-functions-runtimeazure-function-async

Application insights not working with startup DI in Azure Functions


I am trying to log the message from the the Configure class to Application Insights. While the messages are properly logged inside the Run method, but where am adding the polly, Its not sending the message to Appinsights.

//am able to log the message from here.

[FunctionName("Function1")]
public async Task Run(string msg,
            ILogger log)
        {
            log.LogInformation("An error occurred.");
}

// But not from here.

[assembly: FunctionsStartup(typeof(Startup))]
namespace TestFunc2
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.HTTPExtension();
        }
    }

public static class DependencyExtension
{
    public static IServiceCollection HTTPExtension(this IServiceCollection services)
    {
        services.AddHttpClient<Function1>("client", (provider, client) =>
        {
            var logger = provider.GetService<ILogger<Function1>>();
            logger.LogInformation("func2");
            logger.LogError("func2");
            client.BaseAddress = new Uri("http://www.ggl.com");
            client.DefaultRequestHeaders.Add("Accept", "application/json");
        });

        return services;

    }
}

}

Solution

  • I am able to push the logs to App Insights now. I was going through github issues, Here it's informed we need to add logging in the host.json as a workaround to make sure it overrides predefined configuration.

    Here is the config we need to add in the host.json

    {
        "version": "2.0",
        "logging": {
            "logLevel": {
                "Default": "Information"
            }
        }
    }