NLog with Static Class Wrapper and using ${callsite}

1,517 views
Skip to first unread message

Rui Romano

unread,
Mar 23, 2013, 11:06:17 AM3/23/13
to nlog-...@googlegroups.com
Hi,

A have a static logger wrapper that uses a custom ILogger interface that I use to implement a NLog writer, like this:

 public static class Logger
    {
        private static ILogger currentLogger;

        /// <summary>
        /// Gets the current logger.
        /// </summary>
        public static ILogger CurrentLogger
        {
            get
            {
                if (currentLogger == null)
                {
                    currentLogger = new NLogLogger();
                }
                
                return currentLogger;
            }
            set
            {
                currentLogger = value;
            }
        }
}

My NLogLogger implementation is like this:

    public class NLogLogger : ILogger
    {
        private static NLog.Logger logger = null;   

        public void Log(LogTypeEnum logType, Exception ex, string message, params object[] args)
        {
            if (logger == null)
            {
                logger = NLog.LogManager.GetCurrentClassLogger();
            }

            var msg = Logger.FormatMessage(message, args);
                        
            var logLevel = LogLevel.Info;
            
            switch (logType)
            {
                case LogTypeEnum.Warning:
                    logLevel = LogLevel.Warn;
                    break;
                case LogTypeEnum.Error:
                    logLevel = LogLevel.Error;
                    break;
                case LogTypeEnum.Debug:
                    logLevel = LogLevel.Debug;
                    break;
            }

            var formatProvider = System.Globalization.CultureInfo.InvariantCulture;

            var logEvent = new LogEventInfo(logLevel, logger.Name, formatProvider, msg, args, ex);

            // Para que o callsite não tenha o wrapper

            logger.Log(typeof(NLogLogger), logEvent);
        }
    }


My problem is that when I use ${callsite} it always return the static wrapper method: "Logging.Logger.Log"

If I put the static class type in the NLog "logger.Log" call the callsite returns the NLogLogger method.

Is there any way to avoid this and return the caller of "Logger.Log"?

PS - With Log4Net I can do that because they have a parameter: callerStackBoundaryDeclaringType and if I set the Logger type it works well.

Thanks

Kim Christensen

unread,
Mar 23, 2013, 7:13:56 PM3/23/13
to nlog-...@googlegroups.com
Hi Rui,

There are currently no way to do this in NLog, but seems like a very nice enhancement to implement. Please create a feature request on Github Issues, so this could be included in NLog in the near future.
Reply all
Reply to author
Forward
0 new messages