Tip: Structured logging with NLog 5 for ELK stack
Just a small blog example showing new NLog capabilities and JSON layout for logging in ELK stack. For the time of writing NLog 5 is RC2, so the stable version will not be very different from this one.
This sample uses NLog.config file and the new ability to save complex layout in variable along with other changes, so I’ll just leave the config here:
You can also notice the new ability to add message with exception: `${message:withException=true}` so that now you don’t have to create wired layout part just to format the exceptions.
Using this config we get the JSON formatted common layout + objects from structured log as JSON, so that you can index them in Elastic Search later if you want.
Example of adding log (uses structured logging ability):
_logger.LogInformation("New car created: {newCar}", newCar);The `newCar` part will be added as JSON under `eventProperties` in the log.
Full working example on .NET 6 that generates lots of logs with this sample formatting + exception and other samples:
