2
votes

I am logging from an ASP.NET app using NLog. Everything works great except for the ${logger}. My log files are missing the logger name. Here is the target line from NLog configuration file:

<target name="DebugToFile" xsi:type="File" fileName="${basedir}/Debug.log" layout="${logger} | ${longdate} | ${message}" />

Here is what I get in the log file:

| 2013-10-05 17:55:20.9852 | In Decrypt
| 2013-10-05 17:55:20.9852 | In Deserialize

And here is the C# line that creates an instance of the logger:

new Log("Iamalogger").Debug("In Decrypt");

Any ideas why ${logger} isn't getting picked up?

Edit: 1/14/2014 Here is the log wrapper I am using:

public class Log 
{
    private Logger _log;

    public Log(string class)
    {
        _log = LogManager.GetLogger(class);
    }

    //methods for logging...
}
1
Can you show us the code for your Log class?wageoghe
As @wageoghe says, it seems like you are using a customer Log class, is this correct? If so please provide the source code for the classXharze
Sorry, this got pushed down on my priority. Just updated with the code from the Log wrapper I am using.codelove

1 Answers

0
votes

I know this is an old question, but I stumbled upon this problem recently, so I'd like to show what was wrong with my implementation in case someone else finds itself in this situation.

Since the log wrapper in the question doesn't show the implementation of the Debug method, I'll assume it was something like the implementation that I did in my wrapper:

public class Log
{
    private Logger _log;

    public Log (string className)
    {
        _log = LogManager.GetLogger(className);
    }

    public void Debug(string message)
    {
        _log.Log(new LogEventInfo
        {
            TimeStamp = DateTime.UtcNow,
            Level = LogLevel.Debug,
            Message = message,
        });
    }
}

...and, of course, if we use this class like this:

new Log("Iamalogger").Debug("In Decrypt");

the log is missing the logger name:

 | 2018-05-13 15:58:06.1805 | In Decrypt


The solution was to set the value to the LoggerName property in the LogEventInfo:

public void Debug(string message)
{
    _log.Log(new LogEventInfo
    {
        TimeStamp = DateTime.UtcNow,
        Level = LogLevel.Debug,
        Message = message,
        LoggerName = _log.Name
    });
}

or using the constructor that accepts the logger name:

public void Debug(string message)
{
    _log.Log(new LogEventInfo(LogLevel.Debug, _log.Name, message)
    {
        TimeStamp = DateTime.UtcNow
    });
}

resulting in the following log being written:

Iamalogger | 2018-05-13 16:18:16.1201 | In Decrypt