簡體   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