lapsus lapsus - 2 months ago 39
C# Question

How to use the NLog LogMessageGenerator delegate?

If I want to achieve best possible performance with NLog is there anything I should do in my code to achieve this?

(Of course I have async enabled in the NLog.config.)

What about using the delegate to write log messages?

_logger.Info(() => { return "test" });

Unfortunately I couldn't figure out how to use this "properly". Is this even documented?

_logger.Info(() => { return string.Format("msg", myParams); }); // Is that the way to go?


The recommendations are as follows:

  • For a plain string, just pass it _logger.Info("test");
  • For a string with parameters, use the overloads _logger.Info("test with {0}", "value");
  • For an expensive operation, use _logger.Info(() => expensiveOperation); or _logger.Info(() => {expensiveOperation()});
  • When writing extensions/wrappers etc. You can also check _logger.IsInfoEnabled etc. before writing