Skip to content

Latest commit

 

History

History

README.md

Structured Logging Examples in C# — Serilog, Logging Scopes, and ASP.NET Core

YouTube Blog Newsletter All Links LinkedIn

Practical structured logging examples for .NET and ASP.NET Core using Serilog. Learn how to move beyond plain-text logs with queryable key-value properties, how to use logging scopes to add contextual information across multiple log entries, and how to configure Serilog entirely from appsettings.json without touching code.

Watch the Videos

STOP Passing State For Logging: A Practical Example of Logging Scopes

STOP Passing State For Logging: A Practical Example of Logging Scopes

Beginner's Guide To Structured Logging in C#: Logging Scopes

Beginner's Guide To Structured Logging in C#: Logging Scopes

How To Configure Serilog From AppSettings.json

How To Configure Serilog From AppSettings.json

Let's VIBE! ChatGPT vs Copilot — Which Integrates Serilog Better?

Let's VIBE! ChatGPT vs Copilot — Which Integrates Serilog Better?

Projects

Project Description
AspNetCoreSerilogFromAppSettingsExample Demonstrates configuring Serilog sinks, enrichers, and minimum log levels entirely through appsettings.json — no code-based configuration needed
LoggingScopeExample1 Introduces ILogger.BeginScope() to attach contextual properties (e.g. request ID, user ID) to every log entry within a scope block
LoggingScopeExample2 Shows a more advanced logging scope pattern with structured scope state objects
VibeCodeSerilogAspNetCore A vibe-coded ASP.NET Core Web API with full Serilog integration — generated with AI assistance to show real-world structured logging setup

Getting Started

  1. Clone the repository
  2. Open StructuredLoggingExamples.slnx in Visual Studio 2022+ or use dotnet run
  3. Run any project: dotnet run --project AspNetCoreSerilogFromAppSettingsExample
  4. Observe structured log output in the console with enriched properties

Key Concepts

  • Structured Logging — logs as queryable key-value pairs instead of flat strings, enabling filtering and aggregation in tools like Seq or Application Insights
  • Serilog — the most popular structured logging library for .NET with a rich sink ecosystem
  • Logging ScopesILogger.BeginScope() attaches ambient context (correlation IDs, user info) to all log entries within a code block
  • AppSettings Configuration — configure Serilog sinks, enrichers, and levels via appsettings.json for environment-specific logging without redeployment

Newsletter

If you found this useful and you want to learn more about C#, .NET, and software engineering, subscribe to the free Dev Leader Weekly newsletter:

Subscribe to Dev Leader Weekly

Connect with Dev Leader


BrandGhost

Powered by BrandGhost 👻