It’s easy to overlook how small differences in logging syntax can affect performance, log structure, and searchability.
Take this innocent-looking log:
logger.LogInformation("Hello " + name);
Now compare it to:
logger.LogInformation($"Hello {name}");
And finally, this one:
logger.LogInformation("Hello {Name}", name);
They all seem to do the same thing, right?
Well… not quite. Let’s dive into what really sets them apart and why you should care.
1. Logging with String Concatenation
This method involves building log messages by explicitly joining strings and variable values using concatenation operators.
string user = "Alice";
int id = 123;
logger.LogInformation("User " + user + " with ID " + id + " logged in.");
Drawbacks
- You lose the ability to parse or query
name
as a separate field in structured log systems.
- Performance hit, even if logging is disabled at
Information
level, the string is still evaluated in memory.
2. Logging with String Interpolation
String interpolation provides a more concise and readable syntax for embedding expressions.
string user = "Bob";
int id = 456;
logger.LogInformation($"User {user} with ID {id} logged in.");
Drawbacks: while more readable than concatenation,
- The interpolated message is still passed as a single string.
- The string is formatted in memory regardless of the logging level.
3. Structured Logging
Structured logging involves emitting log data in a predefined, machine-readable format like JSON or key-value pairs, rather than plain text.
It separates the log message template from the dynamic data, allowing for richer querying and analysis. example.
string user = "Charlie";
int id = 789;
logger.LogInformation("User {UserName} with ID {UserId} logged in.", user, id);
Advantages
- Log data can be easily queried and filtered based on specific fields.
- Enables efficient ingestion and processing by log aggregation and analysis tools like Serilog and Seq will extract
{Name}
as a named field.
- Parameters are only processed if the log level is enabled.
Benchmarking results
![Benchmarking result]()
Modern .NET logging is more than just writing strings to the console. If you’re using tools like Seq, structured logging unlocks their full power, with better filtering, alerting, and dashboards.
So the next time you’re tempted to log like this:
logger.LogInformation("User logged in: " + userId);
…try this instead:
logger.LogInformation("User logged in: {UserId}", userId);
Your future self and your DevOps team will thank you.