< Summary

Information
Class: AmbientServices.AmbientLogBufferLimits
Assembly: AmbientServices
File(s): /home/runner/work/AmbientServices/AmbientServices/AmbientServices/Helpers/AmbientLogBufferLimits.cs
Tag: 332_35464845198
Line coverage
100%
Covered lines: 9
Uncovered lines: 0
Coverable lines: 9
Total lines: 50
Line coverage: 100%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
EnqueueOrOverflow(...)66.67%66100%

File(s)

/home/runner/work/AmbientServices/AmbientServices/AmbientServices/Helpers/AmbientLogBufferLimits.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2
 3namespace AmbientServices;
 4
 5/// <summary>
 6/// Limits in-memory buffering for ambient log queues. When a queue is full, additional lines are written via the ambien
 7/// </summary>
 8/// <remarks>
 9/// <pitch>The shared guardrail that keeps a logging burst from exhausting process memory: every in-memory log queue in 
 10/// <pledge>Below the cap (100,000 lines by default) the line is enqueued normally; at or above it, the line goes to the
 11/// <plan>A count check against the <see cref="ConcurrentQueue{T}"/> followed by either an enqueue or a swallow-all-exce
 12/// <priority>
 13/// 1. Bounded process memory over never losing a line: past the cap, lines divert to the overflow writer instead of gro
 14/// 2. An approximate cap over an exact one: the check is lock-free, so concurrent writers near the boundary may briefly
 15/// 3. Never throwing over reporting an overflow failure: the call swallows everything, a missing or failing overflow wr
 16/// </priority>
 17/// </remarks>
 18internal static class AmbientLogBufferLimits
 19{
 220    private static readonly AmbientService<IAmbientLogOverflowWriter> _OverflowWriter = Ambient.GetService<IAmbientLogOv
 21
 22    /// <summary>
 23    /// Maximum number of lines held in an in-memory log buffer before additional lines overflow to disk.
 24    /// </summary>
 25    public const int DefaultMaxBufferedLines = 100_000;
 26
 27    /// <summary>
 28    /// Enqueues <paramref name="line"/> when the queue is below the limit; otherwise writes the line via <see cref="IAm
 29    /// </summary>
 30    public static void EnqueueOrOverflow(ConcurrentQueue<string> queue, string line, int maxBufferedLines = DefaultMaxBu
 31    {
 232        if (queue.Count < maxBufferedLines)
 33        {
 234            queue.Enqueue(line);
 35        }
 36        else
 37        {
 38            try
 39            {
 240                IAmbientLogOverflowWriter? writer = _OverflowWriter.Local ?? _OverflowWriter.Global;
 241                writer?.WriteOverflowLine(line);
 242            }
 43#pragma warning disable CA1031
 244            catch
 45#pragma warning restore CA1031
 46            {
 247            }
 48        }
 249    }
 50}