0

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;
                });


            })
MianNajmi
  • 1
  • 1

1 Answers1

0

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["APPINSIGHTS_CONNECTIONSTRING"];

    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.

Zach Bonham
  • 6,759
  • 36
  • 31
  • Just a note - the `_` name for a parameter is generally referred to as the "discard" (see https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/discards) and is meant to signify that you aren't using that parameter in your code. To use it explicitly as you have here will introduce unnecessary confusion for people reading/maintaining your code. – Richard Hauer Jun 13 '23 at 02:00