---
title: Log All Relevant Context On Errors
impact: HIGH
impactDescription: enables quick debugging and incident response
tags: error-handling, logging, context, debugging, quality, csharp
---

## Log All Relevant Context On Errors

Structured logging should include all IDs and state relevant to the error.

**Incorrect (message only):**

```csharp
_logger.LogError("Payment failed");
```

**Correct (structured context):**

```csharp
_logger.LogError(ex, "Payment failed for User {UserId} Order {OrderId} Amount {Amount}", 
    userId, orderId, amount);

// Or using scopes
using (_logger.BeginScope(new Dictionary<string, object> { ["UserId"] = userId }))
{
    _logger.LogError(ex, "Payment failed");
}
```

**Tools:** Serilog, MEL
