---
title: Ensure Exceptions Log Root Cause
impact: CRITICAL
impactDescription: enables debugging of root causes
tags: error-handling, logging, debugging, quality, csharp
---

## Ensure Exceptions Log Root Cause

When catching an exception, always log the full exception object, not just the message.

**Incorrect (swallowing stack trace):**

```csharp
try 
{
    // ...
}
catch (Exception ex)
{
    // Lost stack trace and inner exception!
    _logger.LogError("Error: " + ex.Message); 
}
```

**Correct (logging exception):**

```csharp
try
{
    // ...
}
catch (Exception ex)
{
    // Pass exception object as first argument
    _logger.LogError(ex, "Error processing order {OrderId}", orderId);
}
```

**Tools:** Roslyn Analyzers, SonarQube
