DiagnosticContext Pennington.Diagnostics
Scoped service that accumulates diagnostics for a single HTTP request. Registered as scoped in DI — fresh instance per request, no thread-safety needed.
Properties
DiagnosticsSystem.Collections.Generic.IReadOnlyList<Pennington.Diagnostics.Diagnostic>- Diagnostics accumulated for the current request, in insertion order.
HasAnybool- True when at least one diagnostic has been recorded.
HasErrorsbool- True when at least one recorded diagnostic has
Errorseverity.
Methods
Add
#public void Add(Diagnostic diagnostic);Appends a pre-built diagnostic to the context.
Parameters
diagnosticDiagnostic
AddError
#public void AddError(string message, string? source = null);Records an error-severity diagnostic with the given message and optional source label.
Parameters
messagestringsourcestring? (optional)
AddWarning
#public void AddWarning(string message, string? source = null);Records a warning-severity diagnostic with the given message and optional source label.
Parameters
messagestringsourcestring? (optional)
Pennington.Diagnostics.DiagnosticContext
namespace Pennington.Diagnostics;
/// Scoped service that accumulates diagnostics for a single HTTP request. Registered as scoped in DI — fresh instance per request, no thread-safety needed.
public class DiagnosticContext
{
/// Appends a pre-built diagnostic to the context.
public void Add(Diagnostic diagnostic);
/// Records an error-severity diagnostic with the given message and optional source label.
public void AddError(string message, string? source = null);
/// Records a warning-severity diagnostic with the given message and optional source label.
public void AddWarning(string message, string? source = null);
/// Diagnostics accumulated for the current request, in insertion order.
public IReadOnlyList<Diagnostic> Diagnostics => _diagnostics;
/// True when at least one diagnostic has been recorded.
public bool HasAny => _diagnostics.Count > 0;
/// True when at least one recorded diagnostic has Error severity.
public bool HasErrors => _diagnostics.Exists(d => d.Severity is DiagnosticSeverity.Error);
}