TurboMediator
Observability

Structured Logging

Rich structured logging with sensitive property masking

The structured logging behavior provides detailed, structured logs for every mediator operation with support for sensitive property masking and slow operation detection.

Configuration

builder.Services.AddTurboMediator(m =>
{
    m.WithStructuredLogging<IMessage, object>(options =>
    {
        options.IncludeMessageType = true;
        options.IncludeHandlerName = true;
        options.IncludeDuration = true;
        options.IncludeCorrelationId = true;
        options.IncludeMessageProperties = true;
        options.IncludeResponse = false;
        options.SlowOperationThreshold = TimeSpan.FromSeconds(1);
        options.SensitivePropertyNames = new HashSet<string>
        {
            "Password", "CreditCard", "SSN", "Token"
        };
    });
});

StructuredLoggingOptions

OptionDefaultDescription
IncludeMessageTypetrueLog the message type name
IncludeHandlerNametrueLog the handler class name
IncludeDurationtrueLog execution duration
IncludeCorrelationIdtrueLog correlation ID
IncludeMessagePropertiesfalseLog message property values
IncludeResponsefalseLog response value
SensitivePropertyNamesPassword, Secret, Token, ApiKey, ConnectionString, CreditCard, CardNumber, Cvv, Pin, Ssn, SocialSecurityNumberProperties to mask in logs
SuccessLogLevelInformationLog level for successes
ErrorLogLevelErrorLog level for errors
SlowOperationLogLevelWarningLog level for slow operations
SlowOperationThreshold1 secondDuration to be considered slow
ShouldLognullPredicate to skip logging for certain messages

Sensitive Property Masking

Properties listed in SensitivePropertyNames are replaced with ***REDACTED*** in log output:

// Configuration
options.SensitivePropertyNames = new HashSet<string> { "Password", "Token" };

// Message
public record LoginCommand(string Username, string Password) : ICommand<AuthResult>;

// Log output:
// Handling LoginCommand: { Username = "alice", Password = "***REDACTED***" }

Slow Operation Detection

Operations exceeding SlowOperationThreshold are logged at a higher level:

[Warning] Slow handler detected: GenerateReportQuery took 2345ms (threshold: 1000ms)

Log Output Examples

[Information] Handling CreateUserCommand:
  MessageType: CreateUserCommand
  CorrelationId: abc-123
  Properties: { Name = "Alice", Email = "alice@example.com" }

[Information] Handled CreateUserCommand in 45ms
  HandlerName: CreateUserHandler
  Duration: 45ms

[Error] Error handling CreateUserCommand after 12ms
  HandlerName: CreateUserHandler
  Exception: System.InvalidOperationException: Email already exists

Conditional Logging

Skip logging for specific message types:

options.ShouldLog = (messageType) =>
{
    // Don't log health check queries
    return messageType != typeof(HealthCheckQuery);
};

On this page