1

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;

    }
}

}
threeleggedrabbit
  • 1,722
  • 2
  • 28
  • 60

2 Answers2

1

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"
        }
    }
}
threeleggedrabbit
  • 1,722
  • 2
  • 28
  • 60
0

Had someone run into this yesterday who created this sample - see if that helps:

https://github.com/Runamok81/AzureFunctionHttpClientFactoryPollyLogging

The other thing to be aware of is if you generate your own instance of ILogger for now you need to explicitly add it as a log category in host.json. I don’t know if that’s case here as you are asking for a Function1 instance which I imagine should be added, but for example here I create one with string “Startup”:

https://github.com/jeffhollan/functions-csharp-sqlconnection/blob/b5f1d94e54db96112cbda0257e938e6c887c9310/functions-csharp-sqlconnection/Startup.cs#L19

So I had to update the host.json to make sure it includes this type with logLevel “Information”:

https://github.com/jeffhollan/functions-csharp-sqlconnection/blob/b5f1d94e54db96112cbda0257e938e6c887c9310/functions-csharp-sqlconnection/host.json#L5

jeffhollan
  • 3,139
  • 15
  • 18
  • I have tried the first link, I am able to see the logs in the functions console but not in the App Insights(updated the instrumentation key in the context). Only way which worked for me is using logger factory var loggerFactory.AddApplicationInsights(provider, LogLevel.Information); but loggerFactory.AddApplicationInsights(provider, LogLevel.Information); it is depricated as per MSFT documentation – threeleggedrabbit Sep 15 '19 at 05:54
  • What do you mean when you say you update the instrumentation key in the context? Are you attempting to manually load in your app insights config rather than use the application settings integration? In general for connection strings and instrumentation keys we don’t recommend manually loading in config as can mix with our loaded config – jeffhollan Sep 15 '19 at 06:18
  • 1
    For the testing purpose I did this [var telemetryClient = new TelemetryClient() { InstrumentationKey = "instkey" }; builder.Services.AddSingleton(x => telemetryClient);] to see how it behaves. This works but this is not the recommended way so I had to remove it and look for different option. – threeleggedrabbit Sep 15 '19 at 07:30