Observability

Making ASP.NET Core logs useful when a request fails across services

C# Corner Riya Patel
Observability

When a request crosses an API gateway, an ASP.NET Core service, a database and a payment provider, plain error logs cannot explain what happened. The article combines structured logging via `ILogger<T>` with `ActivitySource` and OpenTelemetry so a single trace carries the request across service boundaries.

Key points

  • Structured logging keeps named properties such as `{OrderId}` as searchable fields instead of buried message text.
  • A trace ID identifies the whole operation while a span ID marks one step inside it, letting tooling rebuild the request path.
  • `ActivitySource.StartActivity()` creates custom operations and `SetTag()` attaches attributes such as order IDs.
  • `AddAspNetCoreInstrumentation()`, `AddHttpClientInstrumentation()` and `AddSource()` cover inbound requests, outbound HTTP calls and custom activities.
  • W3C Trace Context propagates context between services through the `traceparent` and `tracestate` headers.
  • Message-queue communication needs extra instrumentation to carry trace context in message headers; HTTP propagation alone is not enough.

Why it matters

None of this is new machinery: `Activity`, `ILogger<T>` and the OpenTelemetry packages have been in .NET for a while, and the article mostly shows how they fit together. The gap it exposes is real, though. Many teams still log every exception as `Error` and only propagate context over HTTP, so a queue hop or a background worker quietly breaks the trace exactly when it matters.

Before rewriting logging, check two things: whether your log backend can actually query structured properties rather than grepping message text, and whether your `ActivitySource` names are consistent across projects. Then verify that payment, auth and token values never reach the sink; structured logging does not mask sensitive fields on its own.

Real-world use

  • An e-commerce order flow spanning inventory, payment and database services where the charge succeeds but persistence fails.
  • Support engineers filtering by trace ID instead of matching timestamps across hundreds of concurrent requests.
  • A payment or auth path that must log the failing dependency and business ID without writing tokens or card data.

This summary was generated by the agent from the original post on C# Corner. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message