Alan B Alan B - 22 days ago 5
C# Question

NLog writing to log file does not preserve string format

According to the documentation and what everyone else does it should be possible to use a String.Format() approach when writing to the log file. For example, from the documentation:

int k = 42;
int l = 100;

logger.Trace("Sample trace message, k={0}, l={1}", k, l);


I have the following code:

Logger.Info("Server={0}, Port={1}, Interval(ms)={2}, Username={3}, Checking folder={4}",
Config.Server, Config.Port, Config.Username, Config.CSVFolder);


... however the output in the log file is:

2016-11-15 11:40:32.4472 INFO Server={0}, Port={1}, Interval(ms)={2}, Username={3}, Checking folder={4}


In other words the parameters have not been replaced. There are correct values in Config.Server etc, being string, int, string, string respectively.

I can't see anything else I should be doing from the documentation or examples.

Answer

The only wrong thing I can see is the wrong number of parameters... maybe NLog doesn't work if the are not the same?