When interpolating objects into strings (mainly for logging purposes), it needs to be explicitly serialized otherwise what you get is:
<ProjectName>.<Class>
or in my case
ConsoleApp1.Program+Person
So I made a very simple console application as a PoC to tackle this problem.
In this PoC I have an abstract base class that only overrides ToString method with JsonSerializer, so I do not need to serialize every time I want to log/ConsoleWrite my object.
public abstract class BaseModel
{
public override string ToString()
{
return JsonSerializer.Serialize(this);
}
}
This abstract class is supposed to be inherited by all my models. This is the whole console app
static async Task Main(string[] args)
{
var a = new Person() { Name = "John", Lastname = "Doe" };
Console.WriteLine($"Hi, {a}.");
Console.ReadKey();
}
public class Person : BaseModel
{
public string Name { get; set; }
public string Lastname { get; set; }
}
Running the code above ConsoleWrites
Hi, {}.
Why is it empty?
When I put a Quickwatch on this in the abstract method I can see that the properties are populated properly.

Why does this happen?