36 lines
1.0 KiB
C#
36 lines
1.0 KiB
C#
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;
|
|
} |