0
Method storage loggingFramework Mean Error StdDev Rank Gen 0 Gen 1 Gen 2 Allocated
LogInfo HDD Nlog 317.5 ns 68.74 ns 3.77 ns 1 0.0196 0.0067 - 149 B
LogInfo SSD Nlog 321.3 ns 221.72 ns 12.15 ns 2 0.0191 0.0067 - 145 B
LogInfo RamDisk Nlog 326.7 ns 72.90 ns 4.00 ns 3 0.0200 0.0072 - 152 B
LogInfo RamDisk Log4Net 6,246.7 ns 330.27 ns 18.10 ns 4 0.0305 - - 216 B
LogInfo HDD Log4Net 7,874.0 ns 20,773.26 ns 1,138.65 ns 5 0.0305 - - 216 B
LogInfo SSD Log4Net 8,360.2 ns 26,805.20 ns 1,469.28 ns 6 0.0305 - - 216 B
[Benchmark]
public void LogInfo()
{
    //for (int i = 0; i < 100; i++)
    //{
    Logger.Info(depthMessage);
    //}
}

The question is not clear because I am new to log framework. Obviously, log is not written to disk directly, which leads to benchmark failure. I cannot delete question. I share my config here.

<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="false" >
    <variable name='globalLevel' value='info'/>
    <variable name="LogDay" value="${date:format=yyyyMMdd}"/>
    <variable name="LogHour" value="${date:format=yyyyMMdd-HH}"/>
    <variable name="LogHour" value="${date:format=yyyyMMdd-HH}"/>
    <variable name="shortlogtime" value="${date:format=HH\:mm\:ss.fff}"/>
    <targets>
        <target name="SSDRollingFileAppender" xsi:type="File" fileName="c:\Nlog\${LogDay}.log"
                    layout="${shortlogtime}${message}"
                    maxArchiveFiles="100"
                    archiveAboveSize="5368709120"
                    archiveNumbering="Sequence"
                    concurrentWrites="false"
                    keepFileOpen="true"
                    
                    archiveFileName="c:\Nlog\${LogDay}.{##}.log"
                    />


        <target name="HDDRollingFileAppender" xsi:type="File" fileName="d:\Nlog\${LogDay}.log"
                    layout="${shortlogtime}${message}"
                    maxArchiveFiles="100"
                    archiveAboveSize="5368709120"
                    archiveNumbering="Sequence"
                    concurrentWrites="false"
                    keepFileOpen="true"
                    
                    archiveFileName="d:\Nlog\${LogDay}.{##}.log"
                    />

        <target name="RamDiskRollingFileAppender" xsi:type="File" fileName="e:\Nlog\${LogDay}.log"
                    layout="${shortlogtime}${message}"
                    maxArchiveFiles="100"
                    archiveAboveSize="5368709120"
                    archiveNumbering="Sequence"
                    concurrentWrites="false"
                    keepFileOpen="true"
                    
                    archiveFileName="e:\Nlog\${LogDay}.{##}.log"
                    />
    </targets>
    <rules>
        <logger name="SSD" minlevel="${globalLevel}" writeTo="SSDRollingFileAppender" final="true" />
        <logger name="HDD" minlevel="${globalLevel}" writeTo="HDDRollingFileAppender" final="true" />
        <logger name="RamDisk" minlevel="${globalLevel}" writeTo="RamDiskRollingFileAppender" final="true" />
    </rules>
</nlog>
  • 1
    Better than what? – Lasse V. Karlsen Aug 04 '21 at 09:07
  • You can see there's no big difference between HDD and SSD for Nlog. So why? Thanks – Francis Liu Aug 04 '21 at 09:08
  • How are you testing this? Are you making sure that the data gets flushed to disk too? – AKX Aug 04 '21 at 09:09
  • you always need to be careful with what you are measuring. It is likely you are measuring the time to write to some buffer. Also, what is the actual problem? SSDs in general have better performance than spinning disks, but if logging is a performance bottleneck you probably need other fixes. – JonasH Aug 04 '21 at 09:11
  • For Nlog, to have the benchmark make sense you'll probably also want to make sure all loggers are flushed as described in https://stackoverflow.com/a/8248256/51685 – AKX Aug 04 '21 at 09:14
  • No config, no clear question. This is really unclear – Julian Aug 04 '21 at 10:10

1 Answers1

1

NLog FileTarget is by default running with the handbreak pulled. Please configure these settings for the NLog FileTarget and run your benchmark again:

  • KeepFileOpen = true
  • ConcurrentWrites = false

See also: https://github.com/NLog/NLog/wiki/Performance#file-logging-performance

P.S. If you want really fast performance, then you should also enable AsyncWrapper, but remember to flush and also notice that by default overflowAction = Discard.

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70