🔧 Observability & Service Mesh for ASP.NET Core Microservices

Samim.Hossain
Samim Hossain
Published on Apr, 26 2026 2 min read 0 comments
image

Building microservices is great, but without observability, it’s like flying blind.
In this article, we’ll cover monitoring, logging, tracing, and Service Mesh concepts to make your ASP.NET Core microservices production-ready.

Why Observability Matters

  • Detect issues before users report them
  • Monitor service health & performance
  • Trace requests across multiple services
  • Debug failures in distributed systems

Key Pillars:

  1. Metrics → Numbers (CPU, Memory, Response Time)
  2. Logs → Events (errors, warnings, info)
  3. Traces → Request paths across services

Step 1: Structured Logging

Use Serilog for structured logging:

using Serilog;

Log.Logger = new LoggerConfiguration()
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day)
    .CreateLogger();

Example Log

{
  "Timestamp": "2026-01-10T10:15:30Z",
  "Level": "Information",
  "Message": "User logged in",
  "UserId": "1234",
  "Service": "UsersService"
}

✅ Structured logs make analysis easier in ELK/Graylog.

Step 2: Metrics with Prometheus

  • Add prometheus-net NuGet package
  • Expose metrics endpoint:
using Prometheus;

var app = builder.Build();

app.UseMetricServer();        // /metrics endpoint
app.UseHttpMetrics();         // HTTP request metrics

app.Run();
  • Configure Prometheus to scrape /metrics
  • Visualize with Grafana

Step 3: Distributed Tracing with OpenTelemetry

Install OpenTelemetry packages:

dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Exporter.Jaeger
dotnet add package OpenTelemetry.Instrumentation.AspNetCore

Configure:

builder.Services.AddOpenTelemetryTracing(tracerProviderBuilder =>
{
    tracerProviderBuilder
        .AddAspNetCoreInstrumentation()
        .AddHttpClientInstrumentation()
        .AddJaegerExporter(jaegerOptions =>
        {
            jaegerOptions.AgentHost = "jaeger";
            jaegerOptions.AgentPort = 6831;
        });
});
  • Trace requests across services
  • Identify slow calls or failures

Step 4: Health Checks

ASP.NET Core provides built-in Health Checks:

builder.Services.AddHealthChecks()
    .AddSqlServer(connectionString)
    .AddRedis(redisConnection);

app.MapHealthChecks("/health");
  • Kubernetes readiness/liveness probes rely on this
  • Ensures unhealthy pods are restarted automatically

Step 5: Service Mesh Overview

Service Mesh adds advanced networking for microservices:

  • Istio, Linkerd, Consul
  • Handles:
    • Traffic routing
    • Load balancing
    • Security (mTLS between services)
    • Observability (metrics + tracing)

Benefits of a Service Mesh

  • Automatic TLS between services
  • Circuit breakers & retries
  • Centralized observability
  • Traffic shaping / canary deployments

Step 6: Integrate Istio with ASP.NET Core

  • Install Istio in your K8s cluster
istioctl install
  • Inject sidecar into namespaces
kubectl label namespace mynamespace istio-injection=enabled
  • Deploy services normally → Istio automatically adds Envoy sidecar

Traffic Management Example

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: users-service
spec:
  hosts:
    - users-service
  http:
    - route:
        - destination:
            host: users-service
            subset: v1
          weight: 90
        - destination:
            host: users-service
            subset: v2
          weight: 10

✅ Canary deployment: 10% traffic goes to v2

Step 7: Observability Dashboard

  • Prometheus + Grafana → metrics dashboard
  • Jaeger → distributed tracing
  • ELK → log aggregation

Example dashboards:

  • Requests per second
  • Error rate per service
  • Latency per endpoint
  • Trace visualization for a transaction

Step 8: Best Practices

  • Use structured logs & correlation IDs
  • Expose metrics and health endpoints
  • Enable tracing in all services
  • Use sidecar proxies for secure traffic
  • Monitor resource usage (CPU/memory)

Step 9: Security in Microservices

  • Use mTLS via Service Mesh
  • JWT for service-to-service authentication
  • Role & claims-based policies in each service
  • Rotate secrets with K8s Secrets

Step 10: Summary

By adding observability and service mesh:

  • You can detect issues quickly
  • Scale safely
  • Deploy reliably with canary & blue-green strategies
  • Secure inter-service communication

🔜 Next Steps (Optional Advanced Weeks)

  1. Auto-scaling microservices
  2. Multi-cluster Kubernetes deployments
  3. Advanced policy enforcement with OPA
  4. Cloud-native ASP.NET Core patterns

This completes your 15-week advanced ASP.NET Core mastery series, covering:

  • Fundamentals → Weeks 1–6
  • Security → Weeks 7–11
  • Deployment → Weeks 12–13
  • Cloud-native scaling → Weeks 14–15
0 Comments