简体   繁体   English

Serilog 异步 CPU 使用率高达 100%

[英]Serilog async high CPU usage upto 100%

I use Serilog async logging and while doing performance testing with multiple users we found that serilog is taking all the CPU (up to 100%).我使用 Serilog 异步日志记录,在对多个用户进行性能测试时,我们发现 serilog 占用了所有 CPU(高达 100%)。 ASP.net code creates log file using Serilog Async (log.info, log.warning, log.error) and it has logging on all web pages due to security logging requirements. ASP.net 代码使用 Serilog Async(log.info、log.warning、log.error)创建日志文件,并且由于安全日志记录要求,它已登录所有 web 页面。 CPU usage spikes after 2 hours and remains 100% continuously and system is getting major performance hit. CPU 使用率在 2 小时后达到峰值,并且持续保持 100%,系统性能受到重大影响。

We also tested application without SeriLog enabled, the application does not spike up to 100% CPU usage.我们还测试了未启用 SeriLog 的应用程序,该应用程序的 CPU 使用率不会达到 100%。

I would really appreciate if I get any help here.如果我在这里得到任何帮助,我将不胜感激。 I can share code (logger class) I created if needed.如果需要,我可以共享我创建的代码(记录器类)。

在此处输入图像描述

And how the logging is configured:以及如何配置日志记录:

var logConfig = new LoggerConfiguration()
            .MinimumLevel.Information()
            .WriteTo.Async(logSink =>
            {
                logSink.File(
                    logFilePath,
                    rollingInterval: RollingInterval.Day,
                    retainedFileCountLimit: null,
                    outputTemplate: OutputTemplate,
                    hooks: new HeaderWriter(header),
                    flushToDiskInterval: TimeSpan.FromSeconds(10),
                    shared: true
                    );
            });
        Log.Logger = logConfig.CreateLogger();

Some thoughts on further investigation Serilog performance:关于进一步调查 Serilog 性能的一些想法:

  1. Look if there any messages in SelfLog .查看SelfLog中是否有任何消息。 Normally SelfLog is empty.通常 SelfLog 是空的。 If there are any, fix them.如果有,请修复它们。
Serilog.Debugging.SelfLog.Enable(Console.Error);
  1. Serilog parses and caches every template (up to a fixed size limit) https://github.com/serilog/serilog/wiki/Writing-Log-Events#message-template-recommendations . Serilog 解析和缓存每个模板(最大为固定大小限制) https://github.com/serilog/serilog/wiki/Writing-Log-Events#message-template-recommendations Logging templates is better than variable messages:日志记录模板比可变消息更好:
// Don't:
Log.Information("The time is " + DateTime.Now);

// Do:
Log.Information("The time is {Now}", DateTime.Now);

If you log large strings, log them as property to prevent parsing and caching.如果您记录大字符串,请将它们记录为属性以防止解析和缓存。

  1. Don't accidentally destructure objects with inappropriate properties:不要意外地破坏具有不适当属性的对象:
  • of type Task , Stream , etc类型Task , Stream
  • property getter doing a lot of work做很多工作的财产获取者
  • property getter that throws抛出的属性获取器
// Don't

// Destructuring entire Request with @ will generate a lot of useless data
Log.Information("Got request {@HttpRequest}", HttpContext.Request);

if you want to log such complex objects, define Destructure.ByTransforming() or IDestructuringPolicy.如果要记录此类复杂对象,请定义Destructure.ByTransforming()或 IDestructuringPolicy。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM