Nlog 结构化日志记录不适用于 类

Nlog structured logging not working for classes

我有以下代码:

var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
logger.Info("Application starting");

var a = new A() { B = 5, C = "Test123" };
var b = new { B = a.B, C = a.C};

logger.Info("Test: {@value1}", a); // Outputs "Test: A" (Writes namespace, not values)
logger.Info("Test: {@value1}", b); // Outputs "Test: {"a":5, "c":"Sasadasd"}" (Nice)

class A 
{
  public int B;
  public string C;
}

我没有为 类 进行结构化日志记录并将它们转换为 json,而是在对象上调用了 .ToString()。我只是不明白我该怎么办。我在整个互联网上搜索了大约 8 个小时,似乎没有人遇到与我相同的问题。请帮助:(

我的配置文件:

"NLog": {
    "throwConfigExceptions": true,
    "variables": {
      "solution": "A",
      "project": "A.A"
    },
    "targets": {
      "c": {
        "type": "ColoredConsole",
        "layout": "${longdate} ${message}"
      }
    },
    "rules": [
      {
        "logger": "Microsoft.*",
        "maxLevel": "Info",
        "final": true
      },
      {
        "logger": "*",
        "maxLevel": "Warn",
        "writeTo": "c"
      },
      {
        "logger": "*",
        "minLevel": "Error",
        "writeTo": "c"
      }
    ]
  }

您需要 Nlog 的属性来显示 json 值: 如果你使用这个 class 它会起作用 :

public class A 
{
  public int B { get; set; }
  public string C { get;set; } 
}

这里是 Nlog 的代码示例:https://dotnetfiddle.net/8zRtvO