Search code examples
azure-functionsazure-application-insights

Not getting Information or Trace logs in Application Insights in .NET 8 function


I have create a function in VS 2022 using .NET8. Locally its working fine But info logs are not showing in application insights. I am able to see Warning logs and Error logs.

I thought my code could be the issue. I tried with a sample function too, its also not showing any info, or trace logs in Application Insights But locally its showing logs.

using System;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace Schedule
{
    public class Time
    {
        private readonly ILogger _logger;

        public Time(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<Time>();
        }

        [Function("Time")]
        public void Run([TimerTrigger("0 */50 * * * *")] TimerInfo myTimer)
        {
            _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            
            if (myTimer.ScheduleStatus is not null)
            {
                _logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}");
                _logger.LogTrace("testing trace logs in Application Insights");
            }
        }
    }
}
{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            },
            "enableLiveMetricsFilters": true
        }
    }
}

Anyone else also facing any similar situation?


Solution

  • Adding this in my code resolved issue for information log

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

    And adding this to my code resolved issue for trace log :

    .ConfigureAppConfiguration((hostContext, config) =>
        {
            config.AddJsonFile("host.json", optional: true);
        })
        .ConfigureLogging((hostingContext, logging) =>
        {
            logging.AddApplicationInsights(console =>
            {
                console.IncludeScopes = true;
            });
    
            logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
        })