Struct LogRate
Per-call-site throttle, so a failure that recurs once per packet is reported once per interval.
Inherited Members
Namespace: MOSAIC.Diagnostics
Assembly: MOSAIC.dll
Syntax
public struct LogRate
Remarks
Hold one as a private field beside the call site it guards:
private LogRate _warnRate = new(TimeSpan.FromSeconds(2));
...
if (_warnRate.Allow())
Log.Warn("OnlineICA", Name, $"Deflation stalled ({_warnRate.Suppressed} suppressed)");
Deliberately unsynchronised. It is called from data threads, and a lock there would cost more than the problem it solves; under a race the worst outcome is one duplicated line or one slightly wrong suppressed count, neither of which matters to a reader.
A default instance has no interval and therefore allows every call. Always construct one with LogRate(TimeSpan).
Constructors
LogRate(TimeSpan)
Creates a throttle that allows at most one call per minInterval.
Declaration
public LogRate(TimeSpan minInterval)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | minInterval | Minimum gap between two allowed calls. |
Fields
_intervalMs
Declaration
private readonly long _intervalMs
Field Value
| Type | Description |
|---|---|
| long |
_nextTicks
Declaration
private long _nextTicks
Field Value
| Type | Description |
|---|---|
| long |
_pending
Declaration
private int _pending
Field Value
| Type | Description |
|---|---|
| int |
_suppressed
Declaration
private int _suppressed
Field Value
| Type | Description |
|---|---|
| int |
Properties
Suppressed
How many calls were suppressed since the previous allowed one. Read it right after Allow() returns true, so the line that does get logged says what it stands for.
Declaration
public int Suppressed { get; }
Property Value
| Type | Description |
|---|---|
| int |
Methods
Allow()
True at most once per interval, and always true the first time. Never blocks and never allocates, so it is safe on a data thread.
Declaration
public bool Allow()
Returns
| Type | Description |
|---|---|
| bool | True when the caller should log. |