0

We are experiencing some logging issues after upgrading one of our Azure Functions (v4) to .NET 8 and isolated worker; Some (not all) error level logs are only appearing in the function log stream for "App Insights Logs", and not in Application Insights trace logs.

Note that sampling is (or should be) disabled, ref. Azure function app not always send log to application insight.

When running the app locally and connecting it to Application Insights, all the logs appear when querying for traces.

Program.cs:

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureServices(services =>
    {
        // Other service configurations not included
        services.ConfigureFunctionsApplicationInsights();
        services.AddApplicationInsightsTelemetryWorkerService();
    })
    .ConfigureLogging(logging =>
    {
    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);
            }
        });
    })
    .Build();

host.Run();

host.json:

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "default": "Warning",
      "Function": "Information",
      "<project-namespace>": "Information"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": false
      }
    }
  }
}

Edit - added screenshots of log stream and application insights logs. Log stream with error

Application insights logs without error

1

2 Answers 2

0

Some (not all) error level logs are only appearing in the function log stream for "App Insights Logs", and not in Application Insights trace logs.

By using Http trigger function with runtime stack .NET 8.0 isolated getting all types of logs including Error logs in application insights. check the below code and configuration:

Function code:

public class Function1
{
    private readonly ILogger<Function1> _logger;

    public Function1(ILogger<Function1> logger)
    {
        _logger = logger;
    }

    [Function("Function1")]
    public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
    {
        _logger.LogTrace("this is Trace log");
        _logger.LogDebug("this is Debug log");
        _logger.LogWarning("this is Warning log");
        _logger.LogError("this is Error log");
        _logger.LogInformation("C# HTTP trigger function processed a request.");
        return new OkObjectResult("Welcome to Azure Functions!");
    }
}

.cs proj:

<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.0" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />

Program.cs:

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
        services.Configure<LoggerFilterOptions>(options =>
        {
            LoggerFilterRule toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName
                == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");

            if (toRemove is not null)
            {
                options.Rules.Remove(toRemove);
            }
        });
    })
    .ConfigureAppConfiguration((hostContext, config) =>
    {
        config.AddJsonFile("host.json", optional: true);
    })
    .ConfigureLogging((hostingContext, logging) =>
    {
        logging.AddApplicationInsights(console =>
        {
            console.IncludeScopes = true;
        });

        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
    })
    .Build();

host.Run();

host. json:

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "default": "Trace"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"

      },
      "enableLiveMetricsFilters": true
    }
  }
}

The function ran successfully in local.

enter image description here

Output:

enter image description here

  • Check the below for trace logs in application insights.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

The log was simply hiding as a "Exception" instead of a "Trace" log :) Thanks for getting me to check Transaction search!
0

It seems that Application Insights is now (after upgrading) catching the error as an Exception instead of a trace - probably since one of the custom dimensions contain an Exception.

Thanks to @Pavan for getting me to check the transaction search!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.