Azure / Azure/Azure-Functions

Managing log levels for Isolate worker model not working with code sample provided in documentation

Open
#2,566 1 comment 0 reactions 0 assignees View on GitHub
area: documentation
Dominant language
PowerShell
Stars
1.1k
Forks
215
Avg merge
4h 2m
Merged PRs (30d)
1

Description

In the **"Guide for running C# Azure Functions in the isolated worker model"** there is a section on "Managing log levels" here:
https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide?tabs=hostbuilder%2Cwindows#managing-log-levels

In the example for "IHostBuilder", the removing of the defaultRule is done in the "ConfigureLogging" method

``` C#
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services => {
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
})
.ConfigureLogging(logging =>
{
logging.Services.Configure(options =>
{
LoggerFilterRule defaultRule = options.Rules.FirstOrDefault(rule => rule.ProviderName
== "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (defaultRule is not null)
{
options.Rules.Remove(defaultRule);
}
});
})
.Build();

host.Run();
```
However when I'm running my "TestLogging" function, I see the log messages being written to the "Console":
![Image](https://github.com/user-attachments/assets/224bdee0-2a62-436d-8436-3a9b463d53fe)

But not to "Application Insights":
![Image](https://github.com/user-attachments/assets/c83e938f-95ec-42f1-9046-fdb192293f3a)

After moving the same code to the "ConfigureServices" method instead of "ConfigureLogging" everything starts to work as expected.

``` C#
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services => {
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.Configure(options =>
{
LoggerFilterRule defaultRule = options.Rules.FirstOrDefault(rule => rule.ProviderName
== "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (defaultRule is not null)
{
options.Rules.Remove(defaultRule);
}
});
})
.ConfigureLogging(logging =>
{

})
.Build();

host.Run();
```
![Image](https://github.com/user-attachments/assets/5b51c4b0-b11a-4099-901e-eb6a79d2f3eb)

**Please validate if this is expected behavior. If so then kindly update the documentation? Otherwise perform the needed updates in code, so the example documentation starts to work as expected**

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.