ℹ️ Structured logging has been merged into NLog 4.5 beta
Parsing and rendering of message templates. Fully backwards-compatible with Serilog's message template format and String.Format()
.
Most of the time, you'll use NLog.StructuredEvents as an integrated part of NLog 4.5+. There's no additional API required for this: just pass a message template to ILogger.Info()
and any other method that normally accepts a format string:
logger.Info("User {Username} logged in from {IPAddress}", username, ipAddress)
// -> An event with `Username` and `IPAddress` properties
To use NLog.StructuredEvents as a stand-alone parser or renderer, first install the NuGet package:
Install-Package NLog.StructuredEvents -Pre
The TemplateParser.Parse()
method converts a message template string into a Template
object:
var template = TemplateParser.Parse("User {Username} logged in from {IPAddress}");
You can inspect the individual tokens in the template's Literals
and Holes
properties.
To render a template, the values assigned to each Hole
must be provided positionally:
template.Render(CultureInfo.CurrentCulture, "alice", "192.168.10.20");
// -> User "alice" logged in from "192.168.10.20"
Dual licensed: MIT/BSD (choose one ;))