Lessons in this course (8)

1. What to log, log levels and structured logging

Basic★ Must knowLesson 1 of 8
LevelUse forExample
TraceVery detailed internals; may contain sensitive data — off in production"Entering CalculateTax with 3 lines"
DebugDeveloper diagnostics"Cache miss for product 42"
InformationNormal significant events"Order 1001 placed by customer 42 (₹4,500)"
WarningUnexpected but handled; might need attention"Payment gateway slow (3.2 s), retrying"
ErrorA failure of the current operation"Failed to save order 1001" + exception
CriticalThe app or a major part is down"Cannot connect to database — shutting down"

What to log: application start/stop and configuration summary, incoming requests (method, path, status, duration), important business events, external calls (target, duration, result), handled failures (warning) and unhandled exceptions (error, with stack trace), security events (logins, permission denied).

What NOT to log: passwords, tokens, full card numbers, secrets, connection strings, personal data beyond what's necessary (GDPR/DPDP), huge payloads.

Structured logging — log a message template with named properties instead of a pre-built string:

csharp
// ❌ String interpolation — one opaque string, can't query by OrderId, allocates even if the level is disabled
logger.LogInformation($"Order {order.Id} placed by {customer.Id} total {order.Total}");

// ✅ Message template — properties are stored separately and searchable
logger.LogInformation("Order {OrderId} placed by {CustomerId} total {Total}", order.Id, customer.Id, order.Total);

In Seq/App Insights you can then query OrderId = 1001 or Total > 10000 across millions of entries.