Skip to content

StructuredLogging

Tore Nestenius edited this page Apr 9, 2025 · 1 revision

Unstructured vs. Structured Logging

Overview

This tool shows the difference between unstructured logging and structured logging in ASP.NET Core. It writes two log messages using the same data, but in two different ways:

  • Unstructured logging uses string interpolation or formatting, which creates a plain text log message.
  • Structured logging uses message templates with named placeholders and separate arguments, which allows log processing tools to extract values as structured data.

Purpose

Log message format matters. While both styles write output to the logs, only structured logging keeps the data in a form that can be parsed and queried by logging systems like Seq, Elasticsearch, or Azure Monitor.

This tool helps you understand how structured logging works and why it's a better choice for most applications.

How It Works

  • The tool creates a sample user with a name, ID, IP address, and expiration date.
  • It writes two log messages using that data:
    • One with string interpolation (unstructured)
    • One with structured logging using message templates

You can view the result in your log system to compare how each one looks and how the data can (or cannot) be extracted.

Example

Unstructured:

log.LogWarning($"The user {UserName} with id {UserId} has an IP address of {IpAddress} and an expiration date of {ExpireDate}");

Structured:

log.LogWarning("The user {UserName} with id {UserId} has an IP address of {IpAddress} and an expiration date of {ExpireDate}", 
               `UserName, UserId, IpAddress, ExpireDate);
Clone this wiki locally