Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application logs are not emitted directly to Application Insights, but are being relayed through the Function host with host.json taking effect #2935

Open
nlykkei opened this issue Jan 25, 2025 · 0 comments
Labels
Needs: Triage (Functions) potential-bug Items opened using the bug report template, not yet triaged and confirmed as a bug

Comments

@nlykkei
Copy link

nlykkei commented Jan 25, 2025

Description

I'm configuring my Azure Function app to send logs directly to Application Insights, as documented here:

https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide?tabs=hostbuilder%2Cwindows#application-insights

This should allow me to configure logging in the application only (e.g. appsettings.json), without host.json having affect on the Application Insights emitted logs.

However, when running the Function app in Azure, debug logs are not sent to Application Insights, unless I modify host.json with logging.logLevel.Function: "Debug". Doesn't this mean that application logs are actually sent over the Function host, i.e. Worker -> Functions host -> Application Insights instead of Worker -> Application Insights?

How can I sent application logs directly to Application Insights?

I have the required package references in .csproj:

<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.0.0" />

Program.cs:

using System.Reflection;

using LEGO.IAM.AssetGovernance.Application;
using LEGO.IAM.AssetGovernance.Function;
using LEGO.IAM.AssetGovernance.Function.Extensions;
using LEGO.IAM.AssetGovernance.Infrastructure;

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.AddConsole();

    var connectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING");

    if (!string.IsNullOrEmpty(connectionString))
    {
        builder.AddApplicationInsights(
            configureTelemetryConfiguration: (config) => config.ConnectionString = connectionString,
            configureApplicationInsightsLoggerOptions: (options) => { }
        );
    }
});
var logger = loggerFactory.CreateLogger("Startup");

logger.LogInformation("Starting application");

var builder = FunctionsApplication.CreateBuilder(args);
{
    // Configure application
    builder.ConfigureFunctionsWebApplication();

    var environment = builder.Environment.EnvironmentName;
    var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? Environment.CurrentDirectory;
    builder.Configuration.SetBasePath(basePath);

    builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables();

    // Configure services
    builder.Services.AddApplicationInsightsTelemetryWorkerService();
    builder.Services.ConfigureFunctionsApplicationInsights();
    builder.Services.Configure<WorkerOptions>(options => { });

    builder.Services.AddFunction(builder.Configuration, environment);
    builder.Services.AddApplication(builder.Configuration, environment);
    builder.Services.AddInfrastructure(builder.Configuration, environment);

    // Configure logging
    builder.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);
        }
    });
}

var host = builder.Build();
{
    await host.SetupDatabase(logger);
}

await host.RunAsync();

Steps to reproduce

Create a Function app and configure it to sent application logs directly to Application Insights as documented here:

Configure the application with a logging filter for a category set to "Debug" and write debug logs with this category. Only logs with "Information" level or above are sent to Application Insights, as this is the default setting for host.json's logging.logLevel.Function setting

@nlykkei nlykkei added the potential-bug Items opened using the bug report template, not yet triaged and confirmed as a bug label Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Triage (Functions) potential-bug Items opened using the bug report template, not yet triaged and confirmed as a bug
Projects
None yet
Development

No branches or pull requests

1 participant