繁体   English   中英

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

[英]NLog WebService target creates too much connections

我有一个使用 NLog 的 .NET Core 3.1 服务。 这是我的 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>

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

我试图进入 NLog 源代码并注意到 WebService 目标(我使用的)正在使用 HttpWebRequest,我认为这是打开这些大量连接的原因,因为连接不是持久性的。 我想知道是否有一个选项使用 WebService 目标但通过保持连接活动来重用连接?

或者有一个选项可以在 WebService 目标中使用 HttpClient 而不是 HttpWebRequest ?

任何帮助都会很棒。

HttpWebRequest还没有完全准备好 NetCore3.1

  • 微软最初认为HttpWebRequest完全是垃圾,不应该成为 NetCore 平台的一部分。
  • 微软随后承认,将HttpWebRequest添加到 NetCore 将使从 NetFramework 的过渡更容易。 微软还决定HttpWebRequest应该只是HttpClient的一个苗条包装器,其中每个HttpWebRequest创建自己的HttpClient实例,从而杀死 Http-Connection-pooling(忽略KeepAlive = true
  • 微软后来承认,如果实现HttpWebRequest不符合实际文档和预期行为,则会造成不良声誉。 随着 Net50 的发布,Microsoft 解决了最初半生不熟的HttpWebRequest的许多问题。

另见: https://github.com/dotnet/corefx/pull/41462

我可以看到两个方向:

  • 更新到 Net50(来自 NetCore31)并添加proxyType="DefaultWebProxy"作为WebService -target 的选项。
  • 试试NLog.Targets.Http看看它是否能支持你的场景。

暂无
暂无

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

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