Azure Functions Logger with BeginScope for ApplicationInsights

We have an Azure Function app that is enabled for HttpLogging to emit the logs to Azure ApplicationInsights using the following code,

var host = new HostBuilder()            
        .ConfigureFunctionsWorkerDefaults((_, b) =>
        {                
        }, _ => { })
        .ConfigureServices((c, s) =>
        {
            s.AddTransient<HttpLoggingHandler>();
            s.AddHttpClient("WithLogger").AddHttpMessageHandler<HttpLoggingHandler>();

            s.AddApplicationInsightsTelemetryWorkerService();
            s.ConfigureFunctionsApplicationInsights();
        }

Also, we have used the following in our logging handler,

      Dictionary<string, object> scope = new();
      scope.TryAdd("url", requestUri);
      scope.TryAdd("headers", requestHeaders);
      using (_logger.BeginScope(scope))
      {
        _logger.LogInformation("***** Request/Response Logging *****");
      }

But, in the Azure Application Insights, we could only find ‘***** Request/Response Logging *****’ in the message property and not the information that we added in scope when we used the following query in Application Insights Logs,

traces 
| where operation_Name contains "***** Request/Response Logging *****"

Any idea what is it that we are missing?

Leave a Comment