Brn
Brn

Reputation: 1

Azure function app not logging information messages to app insights traces log

Please bear with me as this is my first question.

I have a function app (V4 runtime) with an EventHub trigger created in VS 2022 in C# and deployed to Azure. I have configured app insights so I can log to there from the code.

I am seeing the warning and error messages getting logged to the traces table but not information or trace messages, using a KQL query like this:

traces
| where timestamp > ago(60min) 
| order by timestamp desc

Here is a block of debug code I have in the function app:

            _logger.Log(LogLevel.Information, $"(LOG) Received event count = {events.Length}");
            _logger.LogInformation($"(INFO) Received event count = {events.Length}");
            _logger.LogWarning($"(WARN) Received event count = {events.Length}");
            _logger.LogError($"(ERR) Received event count = {events.Length}");
            _logger.LogTrace($"(TRACE) Received event count = {events.Length}");

I am getting only the (WARN) and (ERROR) logs.

I've tried numerous settings in the host.json and I cant get this to show the information messages.

Per this: https://learn.microsoft.com/en-us/azure/azure-functions/configure-monitoring?tabs=v2#configure-categories

Under the "loglevel" in host.json, I've tried "Function.myfunc.User":"Information" I've tried "Function":"Information", "Function.myfunc":"Information" all to no avail. (I am using myfunc here but in the host.json I have the name of the function). I am assuming I am supposed to use the name of the Function() attribute and not the actual name of the function "app". And yes I have checked that it matches the case exactly of the function name.

I've tried with and without a "default":"Information", still not working.

Per this: Azure Function Application Insights Logging not Honoring Host.json Configuration

It says to remove the default filters, but I have no default filter listed there.

Clearly the app insights connection is working since I am getting some messages.

Note that if I run this in the debugger I get all messages to the console screen as expected.

Also this is a very low volume application (5000 items a day spread out) so performance is not really an issue here.

If there's an easier way to do some simple logging I am open for suggestions.

UPDATED Here is my complete host.json, and I am still getting only warnings and errors:


{
  "version": "2.0",
  "logging": {
    "LogLevel": {
      "Default": "Warning"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}

Thank you.

Upvotes: 0

Views: 311

Answers (1)

amit_g
amit_g

Reputation: 31270

For application insights set the log level under Logging/ApplicationInsights/LogLevel. This is in addition to Logging/LogLevel

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}

Also, update the default behavior of applicationInsights by removing the default filter rule (see Managing log levels)

var applicationInsightsProviderName = "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider";

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
    })
    .ConfigureLogging(logging =>
    {
        logging.Services.Configure<LoggerFilterOptions>(options =>
        {
            var defaultRule = options.Rules.FirstOrDefault(rule => rule.ProviderName == applicationInsightsProviderName);

            if (defaultRule is not null)
            {
                options.Rules.Remove(defaultRule);
            }
        });
    })
    .Build();

Upvotes: 0

Related Questions