0

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.

1 Answer 1

1

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Holy cow! I didn't know that params object[] will take in the rest of the arguments. Thank you sir. You saved the day! And taught me something new
@made_it_ma Welcome to the DotNet-world :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.