user2055886
user2055886

Reputation: 109

NLog WebService target creates too much connections

I have a .NET Core 3.1 service which make use of NLog. Here is my NLog.config code:

<?xml version="1.0" ?>
<nlog autoReload="true"
      xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="AsyncInformation" xsi:type="AsyncWrapper">
      <target type="WebService"
                  name="ws"
                  url="https://log-api.com/log/v1"
                  protocol="JsonPost">
        <parameter name="">
          <layout xsi:type="newrelic-jsonlayout">
            <attribute name="time" layout="${date}" />
            <attribute name="level" layout="${level:upperCase=true}"/>
            <attribute name="message" layout="${message}" />
            <attribute name="host" layout="${machinename}" />
            <attribute name="ActivityId" layout="${activityId}" />
            <attribute name="processId" layout="${processId}" />
            <attribute name="threadid" layout="${threadid}" />
            <attribute name="event-properties" >
              <layout type="JsonLayout" includeAllProperties="true" maxRecursionLimit="0" escapeForwardSlash="true" />
            </attribute>
            <attribute name="exception" layout="${exception}" />
            <attribute name="traceId" layout="${var:traceId}" />
          </layout>
        </parameter>
        <header name="X-License-Key" layout="${environment:LICENSE_KEY}"/>
      </target>
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Information" maxlevel="Error" writeTo="AsyncInformation"/>
  </rules>
</nlog>

My service is hosted in Azure and I can see that code is running and I can see also the logs arrive into in my API. Service performance been hit and the CPU usage is very high (most of the time 100%), I have noticed that our open connections have been increased from 700 to 2,000 (which is the maximum amount of allowed connections) which I believe is the source of the performance issue.

I tried to get into NLog source code and noticed that WebService target (which I use) is using HttpWebRequest which I believe is the reason for these huge amount of connections that being opened because the connection is not persistence. I wonder if there is an option use the WebService target but with reusing the connections by keeping the connections alive?

Or there is an option to use HttpClient instead of HttpWebRequest in the WebService target?

Any help would be great.

Upvotes: 1

Views: 437

Answers (1)

Rolf Kristensen
Rolf Kristensen

Reputation: 19867

HttpWebRequest was not completely ready with NetCore3.1

  • Microsoft initially decided that HttpWebRequest was completely crap and should NOT be part of the NetCore-platform.
  • Microsoft then acknowledged that adding HttpWebRequest to NetCore woul would make the transition easier from NetFramework. Microsoft also decided that HttpWebRequest should just be a slim wrapper for HttpClient, where each HttpWebRequest creates their own HttpClient-instance, thus killing Http-Connection-pooling (Ignoring KeepAlive = true)
  • Microsoft later acknowledged that making an implementation of HttpWebRequest, that fails to meet the actual documentation and expected behavior would give a bad reputation. With the release of Net50 then Microsoft closed many of the issues with its initially half-baked HttpWebRequest.

See also: https://github.com/dotnet/corefx/pull/41462

I can see 2 directions:

  • Update to Net50 (from NetCore31) and add proxyType="DefaultWebProxy" as option for WebService-target.
  • Try the NLog.Targets.Http and see if it can support your scenario.

Upvotes: 2

Related Questions