简体   繁体   English

NLog WebService 目标创建了太多的连接

[英]NLog WebService target creates too much connections

I have a .NET Core 3.1 service which make use of NLog.我有一个使用 NLog 的 .NET Core 3.1 服务。 Here is my NLog.config code:这是我的 NLog.config 代码:

<?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.我的服务托管在 Azure 中,我可以看到代码正在运行,我还可以看到日志到达我的 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.服务性能受到影响并且 CPU 使用率非常高(大部分时间为 100%),我注意到我们的打开连接数已从 700 增加到 2,000 (这是允许的最大连接数),我认为这是源的性能问题。

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.我试图进入 NLog 源代码并注意到 WebService 目标(我使用的)正在使用 HttpWebRequest,我认为这是打开这些大量连接的原因,因为连接不是持久性的。 I wonder if there is an option use the WebService target but with reusing the connections by keeping the connections alive?我想知道是否有一个选项使用 WebService 目标但通过保持连接活动来重用连接?

Or there is an option to use HttpClient instead of HttpWebRequest in the WebService target?或者有一个选项可以在 WebService 目标中使用 HttpClient 而不是 HttpWebRequest ?

Any help would be great.任何帮助都会很棒。

HttpWebRequest was not completely ready with NetCore3.1 HttpWebRequest还没有完全准备好 NetCore3.1

  • Microsoft initially decided that HttpWebRequest was completely crap and should NOT be part of the NetCore-platform.微软最初认为HttpWebRequest完全是垃圾,不应该成为 NetCore 平台的一部分。
  • Microsoft then acknowledged that adding HttpWebRequest to NetCore woul would make the transition easier from NetFramework.微软随后承认,将HttpWebRequest添加到 NetCore 将使从 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 )微软还决定HttpWebRequest应该只是HttpClient的一个苗条包装器,其中每个HttpWebRequest创建自己的HttpClient实例,从而杀死 Http-Connection-pooling(忽略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.微软后来承认,如果实现HttpWebRequest不符合实际文档和预期行为,则会造成不良声誉。 With the release of Net50 then Microsoft closed many of the issues with its initially half-baked HttpWebRequest .随着 Net50 的发布,Microsoft 解决了最初半生不熟的HttpWebRequest的许多问题。

See also: https://github.com/dotnet/corefx/pull/41462另见: 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.更新到 Net50(来自 NetCore31)并添加proxyType="DefaultWebProxy"作为WebService -target 的选项。
  • Try the NLog.Targets.Http and see if it can support your scenario.试试NLog.Targets.Http看看它是否能支持你的场景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM