asp.netnlogstructured-logging

How to implement structured logging when using NLog in wrapper class


I am having trouble figuring out how I can use the structured logging, when NLog is in a wrapper class. I have an asp.net webapp that calls my nlog wrapper class.

It works fine for regular logging ex. logger.Info("Gets to here.") but i can't get it to work for structured logging calls ex. logger.Info("This is structured logging entry @{param1}", new {Status = "processing"})

This is my Wrapper class for NLog(EventLog.LogManager):

    public static void Info(params object[] args)
    {
        private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();            
        Logger.Log(LogLevel.Info, args);

        //This is what I've tried to no avail.
        //var ci = new CultureInfo("en-US");
        //LogEventInfo le = LogEventInfo.Create(LogLevel.Info, Logger.Name, ci, args);
        //le.Parameters = args;
        //string test = le.FormattedMessage;
        //string test1 = string.Format(le.Parameters[0].ToString(), le.Parameters[1].ToString());
        //Logger.Log(typeof(LogManager), le);  
    }

This is my asp.net application that calls the above method:

public ActionResult Index()
    {
        EventLog.LogManager.Info("Test @{param1}", new { OrderId = 2, Status = "Processing" });
        return View();
    }

If anyone can point me in the right direction, it would be greatly appreciated. Thank you in advance.


Solution

  • Try changing into this:

    namespace EventLog
    {
        public static class LogManager
        {
            private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
    
            public static void Info(string message, params object[] args)
            {
               Logger.Info(message, args);
            }
        }
    }