In this lesson, you will learn how to transform unstructured log messages into data-rich, searchable objects that enable automated machine analytics.
As your system scales, traditional string-based logging—where you manually concatenate information into a single human-readable line—becomes a performance and analytical bottleneck. When you log "Order {id} processed by {user} in {time}ms", you are essentially treating data as opaque text. Machine analytics tools struggle to parse these strings, often requiring expensive RegEx operations just to extract the id or time for a dashboard.
Adopting Message Templates
Structured logging (often referred to as "semantic logging") decouples the log message template from the actual data properties. Instead of formatting a string at the source, you pass the data as distinct arguments. Libraries like Microsoft.Extensions.Logging handle this by storing the template and the property values separately in the underlying logging provider.
Consider the difference below:
// Unstructured (Avoid this)
_logger.LogInformation($"Order {orderId} processed by {userId} in {elapsed}ms");
// Structured (Use this)
_logger.LogInformation("Order {OrderId} processed by {UserId} in {ElapsedMs}ms", orderId, userId, elapsed);
Why This Improves Performance and Searchability
When you use structured parameters, the log provider records the message template and a collection of key-value pairs (e.g., OrderId: 123, ElapsedMs: 45).
- Indexing Efficiency: Backend stores like Elasticsearch or Azure Monitor index these properties as individual fields. You can query
ElapsedMs > 500instantly without parsing text. - Reduced Allocations: Because the string formatting is deferred or handled by the logging infrastructure, you avoid unnecessary string interpolation allocations in hot code paths, especially if the log level is currently disabled.
- Machine Correlation: You can aggregate logs by property rather than text pattern. For example, calculating the average processing time per
UserIdbecomes a simple query rather than a complex log-scraping operation.
By treating your logs as structured data rather than raw text, you turn your diagnostic pipeline from a collection of "searchable strings" into a powerful analytical database capable of driving automated alerts and performance reporting.
Key Takeaway: Shift from formatting strings to capturing data properties, allowing your logs to be indexed and queried as structured objects by machine analytics tools.