wasAsked.

How would you add distributed tracing, structured logs, and correlation IDs to a .NET microservices system?

Flipkart · Full Stack Developer · 3-4-years · technical

Published

Year not specified · .NET, Microservices, Software Engineering

Community answers

Posted by wasAsked editorial collection

Propagate W3C trace context across HTTP and messaging, create spans around meaningful dependencies, and put trace/span IDs in structured logs. Use OpenTelemetry so instrumentation can export to different backends. Example: builder.Services.AddOpenTelemetry() .WithTracing(t => t.AddAspNetCoreInstrumentation() .AddHttpClientInstrumentation() .AddEntityFrameworkCoreInstrumentation()); Log stable fields such as operation, outcome, tenant ID, and duration; never log tokens, passwords, full request bodies, or unnecessary personal data. Sample high-volume success traces but retain errors intelligently. Correlation IDs help support, while distributed traces show causal timing. Define alerts from service-level signals—latency, errors, traffic, saturation—and test that context survives queue boundaries.

Backbencher Developer · · 0 likes

Observability in microservices rests on three pillars — traces, logs and metrics — linked by shared identifiers. Distributed tracing with OpenTelemetry - Add the OpenTelemetry SDK to each service: builder.Services.AddOpenTelemetry() .WithTracing(t => t .AddAspNetCoreInstrumentation() .AddHttpClientInstrumentation() .AddSqlClientInstrumentation() .AddOtlpExporter()) .WithMetrics(m => m.AddAspNetCoreInstrumentation().AddRuntimeInstrumentation().AddOtlpExporter()); - ASP.NET Core and HttpClient propagate the W3C traceparent header automatically, so a request that touches five services becomes one trace with spans for each hop. - Add custom spans for important operations with ActivitySource. - For messaging, put trace context into message headers and extract it in consumers (many broker SDKs and MassTransit do this). - Export to Jaeger, Grafana Tempo, Zipkin, Azure Monitor Application Insights or another OTLP backend. Use sampling to control cost. Structured logging - Log with message templates, not string concatenation: logger.LogInformation("Order {OrderId} paid {Amount}", id, amount); - Use Serilog or the built-in logger with a JSON formatter and ship to a central store (Elasticsearch, Loki, Seq, Log Analytics). - Include trace ID and span ID in every log entry (OpenTelemetry logging does this) so logs can be filtered by trace. - Use logging scopes for request-level context such as tenant ID and user ID. Never log secrets or personal data. Correlation IDs - Prefer the W3C trace ID as the correlation ID. If you must support a custom X-Correlation-ID, accept it at the edge (gateway), propagate it to every downstream call and message, and return it in error responses. - Show the ID in Problem Details so support teams can find all related logs. Make it useful - Dashboards for latency, error rate and throughput per service (RED metrics). - Alerts on SLOs, not on every error. - Health checks and dependency maps.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics