1

I have function app that iterates over log files and sends them to Azure Log Analytics using this sample here: https://learn.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?

  • you'll need to clarify this bit `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.` why does every single request (isn't it going to the same logs endpoint?) need a new http client? can't you just use the async methods on the one instance? why not? – John Gardner Apr 25 '22 at 22:25
  • 1
    see also: https://stackoverflow.com/a/65363311/13687 ? – John Gardner Apr 25 '22 at 22:26
  • @JohnGardner it needs a different signature because Azure Monitor API requires the size of the log that I want to post as a header in DefaultRequestHeaders – Sasha Chernin Apr 26 '22 at 08:26
  • why in `defaultrequestheaders`, not just in any individual request's headers? – John Gardner May 03 '22 at 17:31
  • @JohnGardner yeah eventually I changed it and sending logs to Azure Monitor works without any exception. But unfortunately for some reason some logs appears to have duplicates. Not sure if its problem on Azures side cause of high volume ingestion of data. – Sasha Chernin May 03 '22 at 19:00

1 Answers1

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

Matt Douhan
  • 677
  • 3
  • 13