【问题标题】:Slow performance using Nlog when compared to log4net与 log4net 相比,使用 Nlog 的性能较慢
【发布时间】:2019-08-01 20:26:22
【问题描述】:

我们最近将 ASP.NET Core Web API 项目的日志记录框架从 log4net 更新为 NLog,当我们进行性能测试时,请求/秒服务的 NLog 与 NLog 相比要少得多。下面是我的 NLog 配置文件。

请就我们使用 NLog 的性能问题提出任何想法。

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="info"
      internalLogFile="C:\temp\internal-nlog.txt">

  <!-- enable asp.net core layout renderers -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
    <add assembly="SumoLogic.Logging.NLog"/>
  </extensions>

  <!-- the targets to write to -->
  <targets>
    <!-- write logs to console -->
    <target xsi:type="ColoredConsole" name="allConsole" formatMessage="false" layout="${longdate}|${level:uppercase=true}|${message}" />

    <target name="sumoLogic" type="SumoLogicTarget" formatMessage="false" layout="${date:format=yyyy-MM-dd HH\:mm\:ss.fff} ${level}, ${message}${exception:format=tostring}${newline}">
      <Url>#{Logging__SumoLogic__EndpointUrl}</Url>
      <ConnectionTimeout>30000</ConnectionTimeout>
      <SourceName>#{Logging__SumoLogic__SourceName}</SourceName>
      <SourceCategory>#{Logging__SumoLogic__SourceCategory}</SourceCategory>
      <UseConsoleLog>true</UseConsoleLog>
    </target>

    <target name="bufferedSumoLogic" type="BufferedSumoLogicTarget" formatMessage="false" layout="${date:format=yyyy-MM-dd HH\:mm\:ss.fff} ${level}, ${message}${exception:format=tostring}${newline}">
      <Url>#{Logging__SumoLogic__EndpointUrl}</Url>
      <SourceName>#{Logging__SumoLogic__SourceName}</SourceName>
      <SourceCategory>#{Logging__SumoLogic__SourceCategory}</SourceCategory>
      <ConnectionTimeout>30000</ConnectionTimeout>
      <RetryInterval>5000</RetryInterval>
      <MessagesPerRequest>10</MessagesPerRequest>
      <MaxFlushInterval>10000</MaxFlushInterval>
      <FlushingAccuracy>250</FlushingAccuracy>
      <MaxQueueSizeBytes>500000</MaxQueueSizeBytes>
      <UseConsoleLog>true</UseConsoleLog>
    </target>
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <!-- Skip non-critical Microsoft logs and so log only own logs -->
    <logger name="Microsoft.*" maxLevel="Info" final="true" />

    <!-- Write all debug messages to console -->
    <logger name="*" minlevel="#{Logging__ConsoleLog__LevelMin}" writeTo="allConsole" />

    <!-- Write to sumo buffered log -->
    <logger name="*" minlevel="#{Logging__BufferedLog__LevelMin}" maxlevel="#{Logging__BufferedLog__LevelMax}" writeTo="bufferedSumoLogic" />

    <!-- Write to sumo instant log -->
    <logger name="*" minlevel="#{Logging__InstantLog__LevelMin}" maxlevel="#{Logging__InstantLog__LevelMax}" writeTo="sumoLogic" />
  </rules>
</nlog>

【问题讨论】:

  • 我认为这取决于相扑目标。您可以尝试禁用对控制台的日志记录,因为控制台写入速度很慢。
  • 您能否也粘贴您正在使用的 log4net-config 进行比较? formatMessage="false" 是什么意思?你检查过 NLog internalLogFile 的错误/警告吗?配置&lt;UseConsoleLog&gt;false&lt;/UseConsoleLog&gt; 后性能会提高吗?
  • @manideep 尝试禁用控制台目标?
  • @Julian 是的,我也尝试禁用控制台目标。但没有运气。

标签: .net-core log4net nlog dotnet-core-pack


【解决方案1】:

不幸的是,您无法加快目标在内部执行的操作,它们可能正在执行 Web 请求、数据库调用、写入文件等(我想这毕竟是可以的)但是您可以避免阻塞应用程序线程您正在记录的内容,这对我来说是重要的事情。

只需将async="true" 添加到您的targets 元素中,它应该是: &lt;targets async="true"&gt;

以下链接的一点解释:

异步目标包装器允许记录器代码执行更多 快速,通过排队消息并在单独的线程中处理它们。 您应该将花费大量时间的目标包装在 他们的 Write() 方法带有异步目标以加快日志记录。

另外请记住,某些目标在异步模式下无法正常工作(您会通过测试发现,在我的情况下为 RollbarSharp),因此您可以为这些目标创建另一个 targets 部分。

您可以在此处获取更多信息:https://github.com/nlog/NLog/wiki/AsyncWrapper-target

祝你好运!

【讨论】:

    猜你喜欢
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2010-10-17
    • 1970-01-01
    相关资源
    最近更新 更多