Added logging.

This commit is contained in:
2025-10-20 23:32:38 -04:00
parent e0e8945728
commit 3a115bc7b8
18 changed files with 381 additions and 64 deletions

View File

@@ -0,0 +1,36 @@
namespace JSMR.Application.Logging;
public sealed class LogObjectBuilder
{
private readonly Dictionary<string, object> _d = [];
public LogObjectBuilder Add(string key, object? value)
{
if (value is null)
return this;
_d[key] = value;
return this;
}
public LogObjectBuilder AddIfNotEmpty(string key, string? value)
=> string.IsNullOrWhiteSpace(value) ? this : Add(key, value);
public LogObjectBuilder AddIfNotEmpty<T>(string key, IReadOnlyCollection<T>? value, int? preview = null)
{
if (value is null || value.Count == 0)
return this;
if (preview is null || value.Count <= preview)
return Add(key, value);
// Store small preview + count so logs stay compact
return Add(key, new { Preview = value.Take(preview.Value), value.Count });
}
public LogObjectBuilder AddIfNotDefault<T>(string key, T value) where T : struct, IEquatable<T>
=> value.Equals(default) ? this : Add(key, value);
public object Build() => _d;
}