NLog: reference properties in message

148 Views Asked by At

I am using NLog with structured logging for the first time. I user Serilog in the past, and with that I could write something like this:

Serilog.ILogger logger = CreateSerilogLogger();
logger.ForContext("MyProperty", "Some dummy value")
      .Information("This is the value: {MyProperty}");   // prints out: "This is the value: Some dummy value"

but with NLog it seems I cannot reference properties in the log message. This is what I tried to do:

NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
logger.WithProperty("MyProperty", "Some dummy value")
      .Info("This is the value: {MyProperty}");   // prints out: "This is the value: {MyProperty}" !!!

I know I could simply pass the string inside Info method; this is just an example.

Is there a way to achieve this with NLog? Can properties passed with WithProperty method be linked inside the log message?

0

There are 0 best solutions below