I have following code:
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;
}
Instead of structured logging for classes and converting them to json, I got .ToString() called on object instead. I just don't understand what should I do. I searched the entire internet for like 8 hours, and it seems nobody have the same issue as me. Pls help :(
My config file:
"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"
}
]
}
you need properties for Nlog to show the json value : if you use this class it will work :
public class A
{
public int B { get; set; }
public string C { get;set; }
}
here is the code example with Nlog : https://dotnetfiddle.net/8zRtvO