Skip to content

Support for conditional enrichment based on log level #20

@hokagedami

Description

@hokagedami

Feature Request

Add support for conditionally enabling call stack enrichment based on log level to optimize performance in production environments.

Use Case

In production, we typically only need call stack information for warnings, errors, and critical events. Information and debug logs don't usually need this level of detail, and enabling it for all log levels impacts performance.

Proposed API

.Enrich.WithCallStack(config => config
    .EnableForLevels(LogEventLevel.Warning, LogEventLevel.Error, LogEventLevel.Fatal)
    .WithCallStackFormat(useExceptionLikeFormat: true))

Or alternatively:

.Enrich.WithCallStack(config => config
    .EnableWhen(logEvent => logEvent.Level >= LogEventLevel.Warning))

Benefits

  1. Performance: Skip expensive stack trace capture for info/debug logs
  2. Resource usage: Reduce memory and CPU overhead in high-volume scenarios
  3. Flexibility: Different enrichment strategies per environment

Current Workaround

Using multiple logger configurations:

// Verbose logger with call stack
var errorLogger = new LoggerConfiguration()
    .Filter.ByIncludingOnly(e => e.Level >= LogEventLevel.Warning)
    .Enrich.WithCallStack()
    .WriteTo.File("errors.log")
    .CreateLogger();

// Fast logger without call stack  
var infoLogger = new LoggerConfiguration()
    .Filter.ByIncludingOnly(e => e.Level < LogEventLevel.Warning)
    .WriteTo.Console()
    .CreateLogger();

This works but is cumbersome and requires managing multiple loggers.

Priority

Medium-High - would significantly improve production performance while maintaining debugging capabilities where needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions