Nlog not logging the correct logger's name

I instantiate the logger in every class I want to log like this

private static Logger logger = LogManager.GetCurrentClassLogger();

Then I created a static class with multiple helper functions for logging passing the logger object.

Example of a simple one:

    public static void LogMessage(Logger logger, LogLevel logLevel, string message)
    {
        StringBuilder logMessage = new StringBuilder();
        logMessage.AppendLine("Message: " + message);
        logger.Log(logLevel, logMessage);
    }

The problem is even if I created the logger in another class, it logs the name of the current class (the static one). I can see the name of the logger in the property list and it has the right name (the name of the class it's been instantiated) when calling the log method. I tried to pass the logger as ref, but it does the same thing.

Does anyone knows why this happens?

Upvotes: 1

Views: 1145

Answers (1)

Julian
Julian

Reputation: 36849

This is expected behavior. There are some solutions:

  • Just create a static logger every class, which is recommend, or
  • Use LogManager.GetLogger("loggername") and pass your name, or
  • Use the fluent logging which is included in NLog, see news post, or
  • Use the static class in Fody Anotar: LogTo.Debug("TheMessage");

Upvotes: 1

Related Questions