MetroLog icon indicating copy to clipboard operation
MetroLog copied to clipboard

Messages not always formatted

Open bdurrer opened this issue 6 years ago • 0 comments

Problem description Passing an exception to the logger does not log the exception in all cases and the message is not always formatted.

Example

Logger.Info("formatted exception {0}.", new Exception("exception message").Message);
Logger.Info("exception {0}.", new Exception("exception message"));
Logger.Info("param and exception {0}.", "some other param", new Exception("exception message"));

Output:

12|2019-10-16 11:41:01.475|INFO|1|Test|formatted exception exception message. 13|2019-10-16 11:41:01.475|INFO|1|Test|exception {0}.>>>System.Exception: exception message<<< 14|2019-10-16 11:41:01.475|INFO|1|Test|param and exception some other param.

  1. works as expected
  2. works but fails to format
  3. fails to log the exception

Conclusion: Only the methods with signature (string message, params object[] ps); apply formatting Only the methods with signature (string message, Exception ex = null); log the stack trace

Expected behaviour: NLog 4 moved the exception parameter in front of the message format string to avoid the confusion. Log4net has extra methods for logging formatted messages with an exception.

MetroLog Logger's interface has a signature close to log4j and slf4j and should behave like it:

  • The Logger should not make assumptions on wheter an exception is an formatting parameter or whether it has to be logged as an exception.
  • Both variants should check whether the last argument is an exception and pass it on as exception to the targets.

bdurrer avatar Oct 16 '19 12:10 bdurrer