[英]How to implement structured logging when using NLog in wrapper class
當 NLog 在包裝器 class 中時,我無法弄清楚如何使用結構化日志記錄。 我有一個 asp.net webapp,它調用我的 nlog 包裝器 class。
它適用於常規日志記錄。 logger.Info("Gets to here.")
但我無法讓它用於結構化日志調用 ex。 logger.Info("This is structured logging entry @{param1}", new {Status = "processing"})
這是我的 NLog 包裝器 class(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();
}
如果有人能指出我正確的方向,將不勝感激。 先感謝您。
試試改成這樣:
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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.