azureazure-functionsazure-application-insights

Isolated function app does not log traces but only information


I have a function app that only logs informational and higher. This is my Program.cs:

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();

        var contextualization = services.BuildServiceProvider().GetService<IOptions<ExecutionContextOptions>>()?.Value;

        var config = new ConfigMan(contextualization.AppDirectory);
        services.AddSingleton(config);

        var devOpsRepo = new EDWRepo(config.Values.DB_EDW);
        services.AddSingleton<EDWRepo>(devOpsRepo);

    })
    .ConfigureLogging(logging => {
        logging.Services.Configure<LoggerFilterOptions>(options => {
            LoggerFilterRule defaltRule = options.Rules.FirstOrDefault(rule => rule.ProviderName == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
            if (defaltRule is not null)
            {
                options.Rules.Remove(defaltRule);
            }
        });
    })
    .Build();

host.Run();

Host file:

{
  "version": "2.0",
  "extensions": {
    "queues": {
      "messageEncoding": "none"
    }
  },
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      },
      "logLevel": {
        "default": "Trace"
      },
      "enableLiveMetricsFilters": true
    },
    "logLevel": {
      "default": "Trace",
      "Function": "Trace"
    }
  }
}

And function:

[Function(nameof(VesselAddOrUpdate))]
public async Task Run([QueueTrigger("update-or-add-vessel", Connection = "QueueConnection")] QueueMessage vesselJson)
{
    _logger.LogInformation($"FUNKTION HAS RUN INFO", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Information);
    _logger.LogTrace($"FUNKTION HAS RUN TRACE", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Information);
}

And in my AI i can only see information: enter image description here

Function is running on .NET 8.0 Isolated mode. Any suggestions?


Solution

  • Program.cs:

    using Microsoft.Azure.Functions.Worker;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    
    var host = new HostBuilder()
        .ConfigureFunctionsWebApplication()
        .ConfigureServices(services =>
        {
            services.AddApplicationInsightsTelemetryWorkerService();
            services.ConfigureFunctionsApplicationInsights();
            services.Configure<LoggerFilterOptions>(options =>
            {
                LoggerFilterRule toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName
                    == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
    
                if (toRemove is not null)
                {
                    options.Rules.Remove(toRemove);
                }
            });
        })
        .ConfigureAppConfiguration((hostContext, config) =>
        {
            config.AddJsonFile("host.json", optional: true);
        })
        .ConfigureLogging((hostingContext, logging) =>
        {
            logging.AddApplicationInsights(console =>
            {
                console.IncludeScopes = true;
            });
    
            logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
        })
        .Build();
    
    host.Run();
    

    Function1.cs:

     public class Function1
      {
          private readonly ILogger<Function1> _logger;
    
          public Function1(ILogger<Function1> logger)
          {
              _logger = logger;
          }
    
          [Function(nameof(Function1))]
          public void Run([QueueTrigger("hello", Connection = "queueconn")] QueueMessage message)
          {
              _logger.LogInformation($"C# Queue trigger function processed: {message.MessageText}");
              _logger.LogTrace("this is trace log");
          }
      }
    

    host.json:

    {
      "version": "2.0",
      "logging": {
        "logLevel": {
          "default": "Trace"
        },
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true,
            "excludedTypes": "Request"
    
          },
          "enableLiveMetricsFilters": true
        }
      }
    }
    

    I have sent queue message like below:

    enter image description here

    Output:

    enter image description here