In this lesson, you will learn how to design a unified pipeline to stream logs from distributed .NET microservices into a single, searchable repository.
The Problem of Siloed Logs
As you manage microservices across a cluster, logs trapped inside individual container file systems become inaccessible once a pod restarts or scales down. To troubleshoot effectively, you must decouple log generation from log storage. In an advanced architecture, your .NET services should treat logs as event streams rather than local files, allowing an external collector to ship them to a centralized indexing engine like Elasticsearch or Loki.
Implementing the Strategy
Instead of writing to a file, your .NET services should use the ILogger abstraction to output logs to Console. Because your container runtime captures standard output, a sidecar container or a node-level daemon (like Fluentd or Promtail) can intercept this stream without modifying your application code.
To ensure these logs are searchable, you must inject structured metadata. Use Serilog with a JSON formatter to ensure every entry is parsed correctly by your aggregator:
// Configure Serilog to emit structured JSON
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(new RenderedCompactJsonFormatter())
.Enrich.WithProperty("Environment", "Production")
.Enrich.FromLogContext()
.CreateLogger();
By outputting JSON, your central store can index properties like TraceId or TenantId automatically. This allows you to perform cross-service correlation, effectively tracing a single request's path through multiple distributed services.
Aggregation Flow
- Generation: Services log structured JSON to
stdout. - Collection: A DaemonSet (running on each cluster node) watches the container logs.
- Transport: The agent buffers the logs and pushes them to your backend (e.g., Elasticsearch, OpenSearch).
- Analysis: You query the central store to visualize errors or performance bottlenecks across your entire infrastructure.
This pattern is critical for maintaining observability in a distributed system, ensuring that even when a service crashes, the forensic trail remains intact and queryable within your centralized dashboard.
Key Takeaway: Decouple log generation from storage by emitting structured JSON to standard output, allowing node-level collectors to stream data into a searchable, centralized index.