.net-core - 与 log4net 相比,使用 Nlog 的性能较差

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

我们最近将 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>

最佳答案

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

只需添加 async="true"给你的targets元素,它应该是: <targets async="true">

下面链接的一些解释:

Asynchronous target wrapper allows the logger code to execute more quickly, by queuing messages and processing them in a separate thread. You should wrap targets that spend a non-trivial amount of time in their Write() method with asynchronous target to speed up logging.

还要记住,有些目标在异步模式下效果不佳(您会通过测试发现,在我的例子中是 RollbarSharp ),因此您可以创建另一个 targets这些部分。

您可以在这里获得更多信息:https://github.com/nlog/NLog/wiki/AsyncWrapper-target

祝你好运!

关于.net-core - 与 log4net 相比,使用 Nlog 的性能较差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57316542/

相关文章:

angular - 从 3.1 升级后.NET Core 5.0 Azure 部署 CORS 问题

.net-core - 如何将正确的 libssl 版本包含到 yocto-build 中以运行 .net-core 应用程序?

sql-server - 在 SQL Server 中存储 Log4Net 消息

nhibernate - 使用 log4net 清空 NHibernate 日志文件

c# - 将 google 身份验证添加到 **现有** .net core 2 web api 项目

npm - DotNet Core 2.0 WebDeploy 对 npm build 的调用在 Publish 上不起作用

c# - WPF 应用程序中的 Log4Net 设置

c# - 正确实现 NLog 和 Prism

c# - Unity InjectionFactory 不运行

c# - NLog 自定义 LayoutRenderer 用于范围缩进