S. Kendall
S. Kendall

Reputation: 201

Azure Function not logging to Application Insights

I have an Azure Functions project that was deployed to a Function App for dev/test and was successfully writing log messages to Application Insights. I published the same project to a new Function App, in the same Azure subscription and now neither App logs anything to Application Insights.

The project was deployed using Visual Studio (15.8.7 with Azure Functions and Web Jobs Tools 15.9.02009.0).

I tried adding a logger entry to host.json to explicitly set the Function category log level to information.

The Azure Functions and Web Jobs Tools extension is up to date in Visual Studio.

I also tried deleting the APPINSIGHTS_INSTRUMENTATIONKEY application setting, and restarting the App, so that then when I clicked on the Monitor tab for one of the functions it went through the wizard to configure Application Insights. At the end of that though, it displayed the message "your app is offline or the application insights sdk needs updating".

Any ideas what could be wrong?

Upvotes: 18

Views: 20063

Answers (5)

Mateusz Przybylek
Mateusz Przybylek

Reputation: 5915

If you use isolated Azure Functions, warnings and higher levels are captured in Application Insight by default. If you want to change it at first you need to remove default logging filter added by AI: In Program.cs Configure filter

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(s =>
    {
        s.AddApplicationInsightsTelemetryWorkerService();
        s.ConfigureFunctionsApplicationInsights();
        s.Configure<LoggerFilterOptions>(options =>
        {
            // The Application Insights SDK adds a default logging filter that instructs ILogger to capture only Warning and more severe logs. Application Insights requires an explicit override.
            // Log levels can also be configured using appsettings.json. For more information, see https://learn.microsoft.com/en-us/azure/azure-monitor/app/worker-service#ilogger-logs
            LoggerFilterRule toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName
                == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");

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

After that, you can easily configure logging in host.json More about levels

{
  "logging": {
    "logLevel": {
      "default": "Information"
    }
  }
}

Source in ms docs

Upvotes: 15

majita
majita

Reputation: 1326

Another possibility if your function app has vnet integration -

Since your function app is inside a VNET, it is possible that you have the VNET_ROUTE_ALL app setting set to 1 forcing all traffic via the VNET.

If the VNET doesn't have an outbound rule for the AzureMonitor Service Tag, application insight requests won't get through.

Source: https://learn.microsoft.com/en-us/answers/questions/482908/azure-function-logs-not-going-to-app-insight.html

Upvotes: 1

stimms
stimms

Reputation: 44094

Another confusing source of logging not showing up is injecting loggers into components inside your functions project. For instance you can inject a Logger<T> into something in your function

namespace ThingyFunctions{
  public class Thingy
  {
    public Thingy(ILogger<Thingy> log)
    {
        this.log = log;
    }
    [FunctionName(nameof(Thingy)]
    public async Task Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer)
    {
       log.LogInformation("Some message here");
    }
  }
}

That log message will be filtered out by default. You can see it by changing the log level in your host.json

"logging": {
    "logLevel": {
      "default": "Information"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": false
      }
    }
  }

That will capture everything at the Information level. You can also do it by namespace

"logging": {
    "logLevel": {
      "ThingyFunctions": "Information"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": false
      }
    }
  }

I believe you can also do it by function name

"logging": {
    "logLevel": {
      "Function.Thingy": "Information"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": false
      }
    }
  }

Upvotes: 12

Andrey Stukalin
Andrey Stukalin

Reputation: 5949

Even though the OP has resolved his issue I've found out another possibility for logs to suddenly disappear. There is this feature called sampling. It basically says that if there is much to log it'll choose what to log randomly (or semi-randomly). And it's enabled by default. Long story short, in order to configure it one has to adjust the host.json file.

Specifically, in order to disable the sampling completely one has to add the following:

    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": false
            }
        }
    }

Upvotes: 9

S. Kendall
S. Kendall

Reputation: 201

Hmm. Looks like there was a glitch in the matrix. I'm not sure what part of the matrix though - Azure portal/App Insights etc.

Monitoring is working now though.

Looks like it started working around the time I deleted the APPINSIGHTS_INSTRUMENTATIONKEY and reconfigured it, but entries didn't show up in the portal until over an hour later.

Upvotes: 2

Related Questions