简体   繁体   中英

JObject/JToken doesnt Log on NLog

I have a class and some of properties are dynamic, when my class is pass to some action API the method undestand as a JObject, the problem is to Log on NLOG, when I try to do that, my log from my properties Dynamic shows up "[]" example bellow:

myProperty:[

    [[]],
    [],
    []
]

When I try convert all over my class Objet to JObject(Newtonsoft) all my log will be [] like bellow:

{ "time": "2019-05-13 18:12:16.2224", "level": "DEBUG", "JsonProperties": { "log": [[[[[]],[[]],[[]],[[[[]],[[]],[[]]]],[[[[[[[[]],[[]],[[]],[[]],[[]],[[]],[[]],[[]]]]],[[[[]],[[]],[[]],[[]],[[]],[[[[]],[[]],[[]]]],[[[[[]],[[]]],[[[]],[[]]]]],[[[[[]],[[]],[[]]],[[[]],[[]],[[]]]]],[[[[[]],[[]]],[[[]],[[]]]]]]]]]]]],[[[[[]],[[]],[[]],[[[[]],[[]],[[]],[[]]]]]]]] }, "message": "Testando performance no LoggerGenerator.", "log": [[],[]] }

I solved this problem converts JObject to Dictionary and Works perfectly, the problem is overhead to do that.

I need to solve this problem without create my own method and converts to Dictionary

Thanks.

JObject is an IEnumerable and NLog will try to enumerate it.

You can do the following:

logger.Info("Hello {0}", jObject); // No structured logging, becomes string.Format

logger.Info("Hello {$myobj}", jObject); // Structured logging that forces JObject.ToString

logger.Info("Hello {myobj}", jObject.ToString()); // Converts to string upfront

You can also customize how NLog handles special objects (Like JObject) by overriding these:

  • NLog.Config.ConfigurationItemFactory.Default.ValueFormatter
  • NLog.Config.ConfigurationItemFactory.Default.JsonConverter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM