Skip to content
This repository has been archived by the owner on Nov 18, 2017. It is now read-only.

NLog/NLog.StructuredEvents

Repository files navigation


ℹ️ Structured logging has been merged into NLog 4.5 beta


NLog.StructuredEvents Build status codecov

Parsing and rendering of message templates. Fully backwards-compatible with Serilog's message template format and String.Format().

Getting started

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

Parsing

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.

Rendering

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"  

License

Dual licensed: MIT/BSD (choose one ;))