CodexBloom - Programming Q&A Platform

How to ensure consistent logs across multiple microservices using Serilog and Docker?

πŸ‘€ Views: 1 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-07
serilog microservices docker logging elk C#

I'm optimizing some code but Hey everyone, I'm running into an issue that's driving me crazy..... I'm currently working on a microservices architecture where each service is running in its own Docker container. I'm using Serilog for logging, and I want to ensure that the logs are consistent across all services with proper correlation IDs. I’ve set up a centralized ELK stack for log aggregation, but I'm struggling with how to configure Serilog to ensure all log entries include the same format and correlation ID. Here’s a simplified version of my logging configuration in one of the services: ```csharp using Serilog; public class Program { public static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .WriteTo.Console() .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200")) { AutoRegisterTemplate = true, }) .CreateLogger(); // Ensure correlation ID is enriched using (LogContext.PushProperty("CorrelationId", Guid.NewGuid())) { Log.Information("Starting up..."); // Start service... } } } ``` I added a `CorrelationId` property, but when I make calls to other microservices, I find that the correlation ID is not being passed along in the logs of those services. I’ve tried using HTTP headers to pass the correlation ID but have trouble retrieving it in the downstream services. For example, in the calling service, I added: ```csharp client.DefaultRequestHeaders.Add("X-Correlation-ID", correlationId); ``` But when accessing it in the receiving service, I tried: ```csharp var correlationId = HttpContext.Request.Headers["X-Correlation-ID"]; ``` However, I keep getting a `KeyNotFoundException` when the header is missing. I also noticed that the logs from different services do not align well in the ELK interface, making it hard to trace requests. What configurations or patterns should I implement to ensure that correlation IDs are logged consistently across all microservices? Is there a recommended approach for propagating these IDs effectively? I am using .NET 5.0 and the latest versions of Serilog and Serilog.Sinks.Elasticsearch. Any insights would be greatly appreciated! My development environment is Linux. How would you solve this? For reference, this is a production CLI tool. Any suggestions would be helpful. My development environment is Ubuntu 22.04.