ASP.NET Core MVC Interview Question #136
What is structured logging and why is it useful?
Dependency Injection, Services, Error Handling, Logging & Performance Senior Advanced
Quick Interview Answer
Structured logging records named values as properties rather than flattening everything into an already-formatted string. Logging providers and observability systems can then search, filter, aggregate, and correlate events using those properties.
Detailed Explanation
Structured logging records named values as properties rather than flattening everything into an already-formatted string. Logging providers and observability systems can then search, filter, aggregate, and correlate events using those properties.
Message-template placeholders should represent meaningful fields such as OrderId, UserId, or DurationMs. Avoid interpolated strings when doing so would prevent the provider from capturing structured values.
Code Example
_logger.LogInformation(
"Order {OrderId} completed in {DurationMs} ms",
orderId,
durationMs);