Logging and Monitoring in .
NET Core Applications
1. Logging Strategy
Framework Used: Used Serilog for structured logging with sinks like Console, File,
and Seq.
Enrichment: Included contextual data like CorrelationId, UserId, and RequestPath
using [Link].
Log Levels: Configured proper log levels (Information, Warning, Error, Fatal) per
environment (e.g., verbose in dev, warning+ in prod).
Middleware: Implemented Request/Response Logging Middleware to capture incoming and
outgoing data with headers and bodies using MemoryStream.
2. Correlation and Traceability
Generated and propagated a CorrelationId for every request:
Stored it in X-Correlation-ID header.
Used it across microservices for distributed tracing.
Integrated with Serilog using custom middleware.
3. Exception Handling
Registered a global exception handler middleware:
Logs unhandled exceptions.
Returns standard error response (with traceId).
Captures stack trace, exception type, and request context for deep analysis.
4. Monitoring Tools
Application Insights / Prometheus + Grafana:
Tracked performance metrics (response time, failure rates, dependency calls).
Enabled Live Metrics and custom telemetry for business events.
Health Checks:
Implemented /health endpoint using [Link].
Integrated liveness and readiness checks in Kubernetes.
5. Alerting & Observability
Configured alerts for:
High error rate, high latency, service unavailability.
Dashboards showed:
Request volume, HTTP status codes, exception trends.