Why in Serilog, named placeholders should not be numeric values, but numeric placeholders do not generate a warning with Console.WriteLine
.
e.g:
_log.LogInformation("some data - {0} and data2: {1} ", data1, data2);
causes a warning
named placeholders should not be numeric values
and following is working fine without any warnings
_log.LogInformation("some data - {data1} and data2: {data2} ", data1, data2);
But why does this code here not generate any warning?
Console.WriteLine("some data - {0} and data2: {1} ", data1, data2);
And what is the advantages of using structured logging over string interpolation in Serilog?
Background info on structured logging in Serilog and in general (covers what message templates are intended to achieve, which will answer your question far more completely than attempting to replicate that here):
TL;DR: Message Templates are an (technically upward compatible) extension of message template semantics as used in String.Format
, with specific conventions. As the message says: for log messages, you should not use numbered placeholders, use string identifiers for field names instead.
The LogInformation
call in your example is defined by Microsoft.Extensions.Logging
, which shares rules and conventions about how you add fields to structured log messages. If you've wired that to Serilog, then it will need to meet its rules too (which are pretty much identical). Serilog itself will actually permit you to use the numbers as field ids, but you're missing the point (easy to read/query semantically meaningful log messages) if you do that. For this reason the Serilog Analyzer
rules (and probably some MEL based ones too) will encourage you to use names rather than values.