Sensitive data logging

Logging sensitive data in C#

Description

Structured logging captures arguments such as account.Password alongside the message. Those values can reach log collectors and dashboards.

Potential impact

A log reader could reuse a password, session token or external service’s API key.

Remediation

Leave credentials out of logging calls. If events need to be linked, use a non-sensitive correlation ID.

Examples

Before

csharp
logger.LogInformation("Password {Password}", account.Password);

After

csharp
logger.LogInformation("Password [redacted]");

The revised call writes Password [redacted] without reading or passing the password.

References