Azure / Azure/azure-functions-host

Isolated .NET Host is not respecting Log Level Filters in `host.json` or App Settings

Open
#9,259 9 comments 8 reactions 1 assignee Claimed by @RohitRanjanMS View on GitHub
Logging and Metrics
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 12h
Merged PRs (30d)
38

Description

I am trying to disable the Information log messages emitted by the `HttpClient` as they make about ~50% of all logs sent to our Application Insights and are beginning to cost quite a bit on a high volume service. I have tried creating a filter for the `System.Net.Http.HttpClient` category in both the `host.json` and in app settings, but it does not appear to be working either way. I have confirmed this filter works correctly in a standard .NET application, but does not appear to be respected by the Functions host.

I was able to reproduce this in a minimal reproduction project, all the code for this is included below.

**Program.cs**
```cs
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SampleFunctions;

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddHttpClient();
})
.Build();

host.Run();
```

**host.json**
```json
{
"version": "2.0",
"logging": {
"logLevel": {
"System.Net.Http.HttpClient": "Warning"
}
}
}
```

**local.settings.json**
```json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"Logging": {
"LogLevel": {
"System.Net.Http.HttpClient": "Warning"
}
}
}
}
```
**HttpGateway.cs**
```cs
namespace SampleFunctions;

public interface IHttpGateway
{
Task GetPage();
}

public class HttpGateway : IHttpGateway
{
private readonly HttpClient _client;

public HttpGateway(HttpClient client) => _client = client;

public Task GetPage() => _client.GetAsync("https://example.com");
}
```

**TestFunction.cs**
```cs
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;

namespace SampleFunctions;

public class TestFunction
{
private readonly IHttpGateway _gateway;

public TestFunction(IHttpGateway gateway) => _gateway = gateway;

[Function("TestFunction")]
public async Task Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
{
await _gateway.GetPage();

var response = req.CreateResponse(HttpStatusCode.OK);
return response;
}
}
```
#### Repro steps

- Run Function App
- Call `TestFunction` via HTTP Request
- Observe in the Console output that messages are being emitted from the `HttpClient`

Examples of log messages from `HttpClient`
- `Start processing HTTP request GET https://example.com/`
- `Sending HTTP request GET https://example.com/`
- `Received HTTP response headers after 221.7623ms - 200`
- `End processing HTTP request after 234.0382ms - 200`

#### Expected behavior

No Information level messages from `HttpClient` are displayed in the Console output

#### Actual behavior

Information level messages from `HttpClient` are displayed in the Console output

#### Known workarounds

Adding this to the host builder in `Program.cs` seems to have the desired effect, but requires hardcoded logging configuration rather than accepting my configuration settings.

```cs
.ConfigureLogging(logBuilder =>
{
logBuilder.AddFilter("System.Net.Http.HttpClient", LogLevel.Warning);
})
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.