Application Insight in isolated Function app

1.6k views Asked by At

I am facing issue in moving my Function App code from in-process to isolated process. Here is my application insight code which I am using in my startup.cs. I am not using APPINSIGHTS_INSTRUMENTATIONKEY configuration.

class Startup : FunctionsStartup
{
    public override void 
    ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        builder.ConfigurationBuilder
            .AddApplicationInsightsSettings(applicationInsightsInstrumentationConnectionstring);
    }

   public override void Configure(IFunctionsHostBuilder builder)
   {
       builder.Services.AddApplicationInsightsTelemetry();

    }
}

Now I want to move it to program.cs for Function App isolated process. I have tried different ways but it is not writing logs to application insights.

I need to find a solution for Function App isolated process for Application Insights.

i have tried this and it work fine in my local but when i deploy it in Azure, it did not log anything

.ConfigureServices(services =>
{

               services.AddApplicationInsightsTelemetryWorkerService(opt =>
                {
                    opt.ConnectionString = applicationInsightsConectionString;
                    opt.DependencyCollectionOptions.EnableLegacyCorrelationHeadersInjection = true;
                });


            })
1

There are 1 answers

3
Zach Bonham On

I ran into your post while troubleshooting the same issue. I could get "logs" from the TelemetryClient, but I couldn't seem to get ILogger or ILogger<T> to produce any logs.

Environment:

  • .NET 6 Isolated Function
  • Microsoft.ApplicationInsights.WorkerService (v2.21.0)

In Program.cs:

.ConfigureServices(services =>
{
    var configuration = services.BuildServiceProvider().GetService<IConfiguration>();

    if (configuration == null) throw new NullReferenceException(nameof(configuration));

    var appInsightsConnectionString = configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];

    if (string.IsNullOrEmpty(appInsightsConnectionString)) throw new InvalidOperationException("'APPINSIGHTS_CONNECTIONSTRING' not set.  Check the process configuration.");

    services.AddLogging(_ => 
    {
        _.AddApplicationInsights(telemetry =>
        {
            telemetry.ConnectionString = appInsightsConnectionString;
            
        },
        logging =>
        {
            logging.FlushOnDispose = true;
            
        });

        // we want everything - we can control this via configuration but coding it here
        _.AddFilter("", LogLevel.Debug);

    });
};

With the _.AddFilter("", LogLevel.Debug) I started receiving everything I expected. There is a great post here about different ways we can configure log filtering: https://stackoverflow.com/a/64130971/90895.