在包装类中使用NLog时如何实现结构化日志记录

问题描述 投票:0回答:1

当NLog在包装类中时,我很难弄清楚如何使用结构化日志记录。我有一个asp.net网络应用程序,该应用程序调用了我的nlog包装器类。

对于常规日志记录前工作正常。 logger.Info("Gets to here."),但我无法使其用于结构化日志调用(例如ex)。 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();
    }

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

asp.net nlog structured-logging
1个回答
0
投票

尝试更改为此:

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);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.