0

当 NLog 位于包装器类中时,我无法弄清楚如何使用结构化日志记录。我有一个调用我的 nlog 包装类的 asp.net webapp。

它适用于常规日志记录。logger.Info("Gets to here.")但我不能让它为结构化日志调用工作。logger.Info("This is structured logging entry @{param1}", new {Status = "processing"})

这是我的 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);  
    }

这是我调用上述方法的 asp.net 应用程序:

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

如果有人能指出我正确的方向,将不胜感激。先感谢您。

4

1 回答 1

1

试试改成这样:

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);
        }
    }
}
于 2019-09-26T17:09:59.527 回答