Search code examples
c#azure-functionstraceilogger

Debug and Trace logs from Azure Function (.net Core 3.1) not visible in Application Insights


I'm trying to get the Debug and Trace logs to Application Insights via ILogger, but unfortunately it's not going my way.

I've made the simplest of demos and I get the INFO, WARN, & ERR logs to Application Insights - but not anything lower than INFO.

I've found several posts here on the same topic, but none that seems to solve my issue.

Any help is appreciated :)

Thanks

From AI Transaction Search

enter image description here

demo function

public static class AIDemoConfig
{
    [FunctionName("AIDemo")]
    public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
    {   
        log.LogTrace("TRACE: C# HTTP trigger function processed a request.");
        log.LogDebug("DEBUG: C# HTTP trigger function processed a request.");
        log.LogInformation("INFO: C# HTTP trigger function processed a request.");
        log.LogWarning("WARN: C# HTTP trigger function processed a request.");
        log.LogError("ERROR: C# HTTP trigger function processed a request.");

        string responseMessage = "Hello Log World";

        return new OkObjectResult(responseMessage);
    }
}

Startup

public class Startup : FunctionsStartup
{
    public IConfiguration Configuration { get; }
    public Startup() { }

    public override void Configure(IFunctionsHostBuilder builder)
    {
        //Add ApplicationInsights
        string applicationInsightsInstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
        if (!string.IsNullOrEmpty(applicationInsightsInstrumentationKey))
        {
            builder.Services.AddLogging(builder =>
            {
                builder.AddApplicationInsights(applicationInsightsInstrumentationKey);
                builder.SetMinimumLevel(LogLevel.Trace);
                builder.AddFilter<ApplicationInsightsLoggerProvider>(typeof(AIDemoConfig).FullName, LogLevel.Trace);
                builder.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Error);
            });

            builder.Services.AddApplicationInsightsTelemetry();
        }

        builder.Services.AddMvcCore();
        builder.Services.AddOptions();
    }
}

appsettings.json

{
  "Values": {
    "APPINSIGHTS_INSTRUMENTATIONKEY": "4e0bfb46-68a4-4622-942a-0f80920a82a9"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Warning",
      "Microsoft": "Information"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug"
      }
    }
  }
}

Solution

  • I'm afraid you also need to check the host.json file as this section introduced how to set log level like below:

    For logs of Host.Results or Function, only log events at Error or a higher level.

    {
      "logging": {
        "fileLoggingMode": "always",
        "logLevel": {
          "default": "Information",
          "Host.Results": "Error",
          "Function": "Error",
          "Host.Aggregator": "Trace"
        }
      }
    }