与log4net相比,使用Nlog时性能缓慢

3
我们最近将ASP.NET Core Web API项目的日志框架从log4net更新为NLog,但当进行性能测试时,与log4net相比,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>

我认为这取决于 Sumo 的目标。您可以尝试禁用控制台日志记录,因为控制台写入速度较慢。 - Julian
你能否同时粘贴一下你正在使用的log4net-config进行比较?formatMessage="false"是什么意思?你有检查过NLog internalLogFile中是否有错误/警告吗?当你配置<UseConsoleLog>false</UseConsoleLog>时,性能是否会提高? - Rolf Kristensen
@manideep 尝试禁用控制台目标吗? - Julian
@Julian 是的,我也尝试过禁用控制台目标,但没有成功。 - Manideep Kothapalli
1个回答

5

不幸的是,您不能加速目标内部的操作,因为它们可能正在执行Web请求、数据库调用、写入文件等操作(但我想这些都是可以接受的)。但是,您可以避免阻塞应用程序线程进行日志记录,这对我来说很重要。

只需在targets元素中添加 async="true" 即可,如下所示: <targets async="true">

以下是来自下面链接的一些解释:

异步目标包装器通过将消息排队并在单独的线程中处理它们来加快记录器代码的执行速度。您应该使用异步目标来包装在其Write()方法中花费了相当多时间的目标,以加快日志记录。

还要注意,某些目标在异步模式下效果不佳(您可以通过测试发现,例如我的情况是RollbarSharp),因此您可以为这些目标创建另一个targets部分。

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

祝好运!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接