Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog performance

Tags:

c#

logging

nlog

What should the expected overhead be for logging? I have tried this example

 private class Person  {     private static Logger logger = LogManager.GetCurrentClassLogger();     public string Name { get; private set; }     public Person(string name)        {            Name = name;            logger.Info("New person created with name {0}", name);        }   }    List<Person> people = new List<Person>();   for (int i = 0; i < MAXTEST; i++)   {       people.Add(new Person(i.ToString()));   } 

With MAXTEST values of 100,500,1000, 5000

Results in MAXTEST,noLogging, Logging

100,  25ms, 186ms     500,  33ms, 812ms     1000, 33ms, 1554ms 5000, 33ms, 7654ms 

Granted one would probably never log this excessive amount, but it this the performance hit one would expect?

I have also tried using the asyncwrapper in the config

 <target name="asyncFile" xsi:type="AsyncWrapper">    <target name="file" xsi:type="File" fileName="${basedir}/log.txt" />  </target> 
like image 436
Eric Avatar asked Oct 05 '10 22:10

Eric


People also ask

Which is better Serilog or NLog?

Conclusion. Picking between Serilog and NLog is hard since both have a lot of features like structured logging and C# based configuration. We are using Serilog on elmah.io since we are logging to both elmah.io and Elasticsearch. Both sinks work great and are actively maintained.

Which is better NLog or log4net?

There is some small difference between NLog and log4net. NLog is easier to configure, and supports a much cleaner code-based configuration than log4net. I think the defaults in NLog are also more sensible than in log4net.

Is NLog asynchronous?

NLog 1.0 supports asynchronous logging, but there is no good support for asynchronous exception handling. This is because wrappers targets are not capable of receiving exceptions which are raised on other threads.

What are the NLog levels?

Writing Log Messages With NLog The available methods for logging are (in ascending order): Trace, Debug, Info, Warn, Error, and Fatal. Each of these methods creates a log message with a corresponding log level—an indicator of the message's importance.


1 Answers

You only need to add the async attribute to your targets element:

<targets async="true">         <target name="file" xsi:type="File" fileName="${basedir}/log.txt" /> 

instead of

<targets>     <target name="asyncFile" xsi:type="AsyncWrapper">         <target name="file" xsi:type="File" fileName="${basedir}/log.txt" />     </target> 

I guess I didn't get that far into the documentation ;-)

Asynchronous target wrapper allows the logger code to execute more quickly, by queueing 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. Because asynchronous logging is quite a common scenario, NLog supports a shorthand notation for wrapping all targets with AsyncWrapper. Just add async="true" to the element in the configuration file. ... your targets go here ...

Keep in mind that using async logging can cause certain messages to be discarded. This is by design.


ref: https://github.com/nlog/NLog/wiki/AsyncWrapper-target#async-attribute-and-asyncwrapper

Async attribute and AsyncWrapper

Don't combine the Async attribute and AsyncWrapper. This will only slow down processing and will behave unreliably.

Async attribute will discard by default

The async attribute is a shorthand for:

xsi:type="AsyncWrapper overflowAction="Discard" queueLimit="10000" batchSize="100" timeToSleepBetweenBatches="50"

like image 137
Eric Avatar answered Sep 23 '22 17:09

Eric