Azure Function, Logging part III

.net8, what happened to application insight

With .net8 we have to remove the default Application Insights logging filter to be able to see our logger.loginformation. This is easily resolved by adding the following line of code in your program.cs

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

By utilizing extension I moved this to a custom HostBuilderExtension.cs

public static void RemoveDefaultApplicationInsightsLoggingFilter(this ILoggingBuilder logging)
{
    logging.Services.Configure<LoggerFilterOptions>(options =>
    {
        var defaultRule = options.Rules.FirstOrDefault(rule => rule.ProviderName
            == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
        if (defaultRule is not null)
            options.Rules.Remove(defaultRule);
    });
}

And from my program.cs

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureLogging(logging =>
    {
        logging.RemoveDefaultApplicationInsightsLoggingFilter();
    })
    .Build();
host.Run();

One other thing! Loggs created using logger.loginformation will not be prefixed with “prop__” in Application Insight anymore

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top