elsa-workflows/elsa-core · error · ArgumentOutOfRangeException

Capacity must be greater than zero.

Error message

Capacity must be greater than zero.

What it means

The InMemory structured-log RingBuffer constructor validates that capacity is strictly greater than zero and throws ArgumentOutOfRangeException otherwise. A ring buffer with no capacity cannot hold any entries, so the library rejects it eagerly rather than silently dropping all logs.

Solutions

  1. Pass a positive capacity, e.g. new RingBuffer(1000).
  2. If capacity comes from options, validate/default it: capacity = options.Capacity > 0 ? options.Capacity : 1000.
  3. Fix the configuration source so the bound value is a positive integer.

Example fix

// before
var buffer = new RingBuffer(options.Capacity);
// after
var capacity = options.Capacity > 0 ? options.Capacity : 1000;
var buffer = new RingBuffer(capacity);
Defensive patterns

Strategy: validation

Validate before calling

if (capacity <= 0) throw new Error('RingBuffer capacity must be greater than zero; got ' + capacity); const buffer = new RingBuffer(capacity);

Type guard

function isValidCapacity(v) { return typeof v === 'number' && Number.isInteger(v) && v > 0; }

Try / catch

try { buffer = new RingBuffer(options.Capacity); } catch (ArgumentOutOfRangeException ex) { buffer = new RingBuffer(DefaultCapacity); logger.LogWarning(ex, 'Falling back to default log capacity'); }

Prevention

When it happens

Trigger: Constructing new RingBuffer(0) or new RingBuffer(negative), typically when capacity comes from configuration (e.g. structured log options) that is unset, zero, or parsed incorrectly.

Common situations: An appsettings value like StructuredLogs:Capacity left at 0, a config binder defaulting a missing int to 0, or passing a computed capacity that underflows.

Related errors


AI-assisted analysis of elsa-workflows/elsa-core@fe9217bdfa (2026-09-13). Data as JSON: /api/errors/ebdd169cdd16493c. Report an issue: GitHub.

Appendix: source

Thrown at src/modules/Elsa.Diagnostics.StructuredLogs/Providers/InMemory/RingBuffer.cs:12

namespace Elsa.Diagnostics.StructuredLogs.Providers.InMemory;

public class RingBuffer<T>
{
    private readonly Queue<T> _items = new();
    private readonly object _lock = new();
    private readonly int _capacity;
    
    public RingBuffer(int capacity)
    {
        if (capacity <= 0)
            throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than zero.");
        
        _capacity = capacity;
    }
    
    public long DroppedCount { get; private set; }
    
    public void Add(T item)
    {
        lock (_lock)
        {
            if (_items.Count == _capacity)
            {
                _items.Dequeue();
                DroppedCount++;
            }
            
            _items.Enqueue(item);
        }

View on GitHub (pinned to fe9217bdfa)