Added logging.
This commit is contained in:
36
JSMR.Application/Logging/LogObjectBuilder.cs
Normal file
36
JSMR.Application/Logging/LogObjectBuilder.cs
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user