![](/img/trans.png)
[英]JsonLayout in webservice target without root parameter in NLog
[英]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
我可以看到兩個方向:
proxyType="DefaultWebProxy"
作為WebService
-target 的選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.