Working with logging frameworks
In this tutorial, we'll see how to log instances of value objects.
Writing to the console
In a new console application, add a NuGet reference to Vogen
, and create:
Now, create an instance and write it to the console:
As expected, you'll see:
Vogen generates a default ToString
method that looks like this:
If the value is uninitialized, for example, through deserialization, then it writes uninitialized, otherwise, it uses the underlying type's ToString
method.
It is possible to override ToString
—for example, you might want to pad the value with zeroes. Add the following method to Age
above:
That will now print:
Using the default logging framework
Next, we'll log values out using the default .NET logging framework.
Add NuGet references to:
Microsoft.Extensions.Logging
Microsoft.Extensions.Logging.Console
Next, add a namespace reference to Microsoft.Extensions.Logging
and create an instance of a logger that writes to the console:
Now, change the Console.WriteLine
to:
The output is something like this:
So far, we've used the standard .NET logger to write to a console, and we've written a value object using structured logging.
Next, we'll look more at structured logging and switch to Serilog, a common choice for .NET.
Structured logging with Serilog
Structure logging is supported by the default .NET logger. The structured properties are inside 'index placeholders', for example, {Age}
above. The default logger supports structured logging. However, it can't fully use structured data. For this, we'll use Serilog.
create a new console app
add NuGet packages for
Vogen
,Serilog
, andSerilog.Sinks.Console
create the
Age
value object from above
Add the following initialization:
Run it again, and you'll see something like this:
Let's create a bit more structure. Add a Person
object:
Replace what is logged with:
You'll see:
In Serilog and some other .NET logging libraries that support structured logging, the @
character in a placeholder is called the destructuring operator. When you use the @ character before a placeholder, it tells the logging system to serialize the object completely instead of just calling ToString(). This means the complete state of the object is logged, which can give you much more useful information for complex objects.
In our simple example, changing the logging line to:
Results in:
You can see that Name
is written as: Joe
. But Age
is written as: {"Value": 42, "$type": "Age"}
You might find this output too detailed for value objects and that it pollutes the logs.
We can tell Serilog to use the simpler format for value objects generated by Vogen.
Add the following Destructure
line to the initialization:
And add this type to the project:
Now, run again, and see that our value object Age
is written more simply:
In this tutorial, we've seen how to log value objects to the console and to the default logger. We've also seen how to use structured logging and how to customize the output of value objects written to Serilog.