'Azure function app that sends log data to Azure Monitor causes socket exhaustion

I have function app that iterates over log files and sends them to Azure Log Analytics using this sample here: https://docs.microsoft.com/en-us/azure/azure-monitor/logs/data-collector-api#c-sample

The problem is that I have many logs, so many instances of HttpClient are created, which causes "An attempt was made to access a socket in a way forbidden by its access permissions. ---> System.Net.Sockets.SocketException" error.

I know that is a better practice to create a static instance of HttpClient, but since such instance needs a different signature for each log in my case, its not possible.

Any ideas how can I overcome this error?



Solution 1:[1]

Why does it have to be in the default headers? you can create a httpRequestMessage and change the headers for each connection and still use a static client, the receiver does not know if you used defaultHeaders or not when creating the request.

This is fairly described here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matt Douhan