mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-01-18 18:14:20 +01:00
Added collections from older lib
This commit is contained in:
parent
1011e34d69
commit
0eb939c755
|
@ -6,7 +6,6 @@ using Serilog;
|
||||||
using ILogger = Serilog.ILogger;
|
using ILogger = Serilog.ILogger;
|
||||||
|
|
||||||
var serilogLogger = SetupSerilog();
|
var serilogLogger = SetupSerilog();
|
||||||
|
|
||||||
LogService.AddLogAdapter(new SerilogAdapter(serilogLogger));
|
LogService.AddLogAdapter(new SerilogAdapter(serilogLogger));
|
||||||
|
|
||||||
var logger = LogService.RegisterLogger(typeof(Program));
|
var logger = LogService.RegisterLogger(typeof(Program));
|
||||||
|
|
|
@ -38,6 +38,7 @@ public class SerilogAdapter : LogAdapterBase
|
||||||
}
|
}
|
||||||
switch (capsule.Severity)
|
switch (capsule.Severity)
|
||||||
{
|
{
|
||||||
|
case LogSeverity.Verbose:
|
||||||
case LogSeverity.Trace:
|
case LogSeverity.Trace:
|
||||||
default:
|
default:
|
||||||
logger.Write(new LogEvent(capsule.TimeStamp, LogEventLevel.Verbose, null, template, properties ?? ArraySegment<LogEventProperty>.Empty, ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom()));
|
logger.Write(new LogEvent(capsule.TimeStamp, LogEventLevel.Verbose, null, template, properties ?? ArraySegment<LogEventProperty>.Empty, ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom()));
|
||||||
|
|
97
DotBased/Collections/InstanceContainer.cs
Normal file
97
DotBased/Collections/InstanceContainer.cs
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
using DotBased.Logging;
|
||||||
|
|
||||||
|
namespace DotBased.Collections;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Container to store instances
|
||||||
|
/// <remarks>WIP!</remarks>
|
||||||
|
/// </summary>
|
||||||
|
public class InstanceContainer : IDisposable
|
||||||
|
{
|
||||||
|
private readonly ILogger _log = LogService.RegisterLogger(typeof(InstanceContainer));
|
||||||
|
private readonly Dictionary<string, InstanceNode> _tCollection = new Dictionary<string, InstanceNode>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Register a instance.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>The instace will be created by the <see cref="Get{TInstance}"/> function</remarks>
|
||||||
|
/// <param name="key">Key to indentify the instance</param>
|
||||||
|
/// <typeparam name="TInstance">The instance type</typeparam>
|
||||||
|
public void Register<TInstance>(string key) => _tCollection.Add(key, new InstanceNode(null, typeof(TInstance)));
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add an already constructed instance to the container.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key">Key to identify instance</param>
|
||||||
|
/// <param name="instance">Constructed instance</param>
|
||||||
|
public void Add(string key, object instance) => _tCollection.Add(key, new InstanceNode(instance, instance.GetType()));
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remove a instance from the container.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key">Key to get the instance</param>
|
||||||
|
/// <param name="dispose">Dispose the instance if it inherits the 'IDisposable' interface</param>
|
||||||
|
public void Remove(string key, bool dispose = true)
|
||||||
|
{
|
||||||
|
if (!_tCollection.TryGetValue(key, out var iNode))
|
||||||
|
return;
|
||||||
|
switch (iNode.Instance)
|
||||||
|
{
|
||||||
|
case null:
|
||||||
|
break;
|
||||||
|
case IDisposable instance when dispose:
|
||||||
|
_log.Debug("Disposing disposable object... (InstanceContainer)");
|
||||||
|
instance.Dispose();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_tCollection.Remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get the instance that is stored in the container.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>If the instance is not yet constructed this wil activate the instance</remarks>
|
||||||
|
/// <param name="key">Key to identify the instance</param>
|
||||||
|
/// <typeparam name="TInstance">Instasnce type</typeparam>
|
||||||
|
/// <returns>The instance that is at the given key</returns>
|
||||||
|
public TInstance? Get<TInstance>(string key)
|
||||||
|
{
|
||||||
|
if (!_tCollection.TryGetValue(key, out InstanceNode node) || node.InstanceType != typeof(TInstance))
|
||||||
|
return default;
|
||||||
|
if (node.Instance != null)
|
||||||
|
return (TInstance)node.Instance;
|
||||||
|
node.Instance = Activator.CreateInstance(node.InstanceType);
|
||||||
|
// Override the old node with the new data, else the next 'Get' will reactivate a another instance.
|
||||||
|
_tCollection[key] = node;
|
||||||
|
if (node.Instance != null) return (TInstance)node.Instance;
|
||||||
|
_log.Warning("Instance is null!");
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
foreach (var kvp in _tCollection)
|
||||||
|
{
|
||||||
|
switch (kvp.Value.Instance)
|
||||||
|
{
|
||||||
|
case null:
|
||||||
|
continue;
|
||||||
|
case IDisposable disposable:
|
||||||
|
_log.Verbose("Disposing: {Key}", kvp.Key);
|
||||||
|
disposable.Dispose();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
internal struct InstanceNode
|
||||||
|
{
|
||||||
|
public InstanceNode(object? instance, Type instanceType)
|
||||||
|
{
|
||||||
|
Instance = instance;
|
||||||
|
InstanceType = instanceType;
|
||||||
|
}
|
||||||
|
public object? Instance;
|
||||||
|
public readonly Type InstanceType;
|
||||||
|
}
|
45
DotBased/Collections/KeyContainerBase.cs
Normal file
45
DotBased/Collections/KeyContainerBase.cs
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
|
||||||
|
namespace DotBased.Collections;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Base class for creating containers that has tree like keys
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="separator">The separator used for the keys</param>
|
||||||
|
/// <typeparam name="TContainer">Container data value</typeparam>
|
||||||
|
public abstract class KeyContainerBase<TContainer>(char separator = '.')
|
||||||
|
where TContainer : KeyContainerBase<TContainer>, new()
|
||||||
|
{
|
||||||
|
private readonly Dictionary<string, TContainer> _containers = new Dictionary<string, TContainer>();
|
||||||
|
public ReadOnlyDictionary<string, TContainer> Containers => new ReadOnlyDictionary<string, TContainer>(_containers);
|
||||||
|
|
||||||
|
public TContainer this[string key]
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (key.Contains(separator))
|
||||||
|
return AddFromQueue(new KeyQueue(key, separator));
|
||||||
|
if (!_containers.ContainsKey(key))
|
||||||
|
AddContainer(key, new TContainer());
|
||||||
|
return _containers[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void AddContainer(string key, TContainer container) => _containers[key] = container;
|
||||||
|
public bool RemoveContainer(string key) => _containers.Remove(key);
|
||||||
|
public TContainer GetContainer(string key) => _containers[key];
|
||||||
|
|
||||||
|
TContainer AddFromQueue(KeyQueue queue)
|
||||||
|
{
|
||||||
|
if (queue.IsEmpty) return (TContainer)this;
|
||||||
|
string queueKey = queue.Next();
|
||||||
|
AddContainer(queueKey, new TContainer());
|
||||||
|
return _containers[queueKey].AddFromQueue(queue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
internal class KeyQueue(string key, char divider)
|
||||||
|
{
|
||||||
|
public string Next() => _key.Dequeue();
|
||||||
|
public bool IsEmpty => _key.Count <= 0;
|
||||||
|
|
||||||
|
private readonly Queue<string> _key = new Queue<string>(key.Split(divider, StringSplitOptions.RemoveEmptyEntries));
|
||||||
|
}
|
74
DotBased/Collections/PropertyContainer.cs
Normal file
74
DotBased/Collections/PropertyContainer.cs
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
|
||||||
|
namespace DotBased.Collections;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Property container to store string, long, int, double and bool properties
|
||||||
|
/// </summary>
|
||||||
|
public class PropertyContainer : KeyContainerBase<PropertyContainer>
|
||||||
|
{
|
||||||
|
public PropertyContainer()
|
||||||
|
{ }
|
||||||
|
public PropertyContainer(char separator) : base(separator)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
private readonly Dictionary<string, object> _data = new Dictionary<string, object>();
|
||||||
|
public ReadOnlyDictionary<string, object> Data => new ReadOnlyDictionary<string, object>(_data);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set a property with the corresponding key.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key">The key that will correspond to the property</param>
|
||||||
|
/// <param name="sValue">The string property value</param>
|
||||||
|
/// <param name="overridable">If the key already exists overwrite the property</param>
|
||||||
|
public void Set(string key, string sValue, bool overridable = true)
|
||||||
|
{
|
||||||
|
if (ContainsKey(key) && !overridable)
|
||||||
|
return;
|
||||||
|
_data[key] = sValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Set(string key, long lValue, bool overridable = true)
|
||||||
|
{
|
||||||
|
if (ContainsKey(key) && !overridable)
|
||||||
|
return;
|
||||||
|
_data[key] = lValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Set(string key, double dValue, bool overridable = true)
|
||||||
|
{
|
||||||
|
if (ContainsKey(key) && !overridable)
|
||||||
|
return;
|
||||||
|
_data[key] = dValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Set(string key, bool bValue, bool overridable = true)
|
||||||
|
{
|
||||||
|
if (ContainsKey(key) && !overridable)
|
||||||
|
return;
|
||||||
|
_data[key] = bValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Check if the key exists in this container.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key">The key to check</param>
|
||||||
|
/// <returns>True if the key exists otherwise false</returns>
|
||||||
|
public bool ContainsKey(string key) => _data.ContainsKey(key);
|
||||||
|
|
||||||
|
public string GetString(string key) => Convert.ToString(_data[key]) ?? string.Empty;
|
||||||
|
public long GetLong(string key) => Convert.ToInt64(_data[key]);
|
||||||
|
public double GetDouble(string key) => Convert.ToDouble(_data[key]);
|
||||||
|
public bool GetBool(string key) => Convert.ToBoolean(_data[key]);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes the property at the passed key.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key">The key where to remove the property</param>
|
||||||
|
/// <returns>True is the property is removed otherwise false</returns>
|
||||||
|
public bool Remove(string key) => _data.Remove(key);
|
||||||
|
/// <summary>
|
||||||
|
/// Clears all the properties in this container.
|
||||||
|
/// </summary>
|
||||||
|
public void ClearData() => _data.Clear();
|
||||||
|
}
|
|
@ -7,8 +7,4 @@
|
||||||
<LangVersion>default</LangVersion>
|
<LangVersion>default</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Collections\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -5,6 +5,8 @@ namespace DotBased.Logging;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface ILogger
|
public interface ILogger
|
||||||
{
|
{
|
||||||
|
public void Verbose(string message, params object?[]? parameters);
|
||||||
|
|
||||||
public void Trace(string message, params object?[]? parameters);
|
public void Trace(string message, params object?[]? parameters);
|
||||||
|
|
||||||
public void Debug(string message, params object?[]? parameters);
|
public void Debug(string message, params object?[]? parameters);
|
||||||
|
|
|
@ -2,10 +2,11 @@ namespace DotBased.Logging;
|
||||||
|
|
||||||
public enum LogSeverity
|
public enum LogSeverity
|
||||||
{
|
{
|
||||||
Trace = 0,
|
Verbose = 0,
|
||||||
Debug = 1,
|
Trace = 1,
|
||||||
Info = 2,
|
Debug = 2,
|
||||||
Warning = 3,
|
Info = 3,
|
||||||
Error = 4,
|
Warning = 4,
|
||||||
Fatal = 5
|
Error = 5,
|
||||||
|
Fatal = 6
|
||||||
}
|
}
|
|
@ -10,6 +10,18 @@ public class Logger(CallerInformation caller, ref Action<LogCapsule> logProcesso
|
||||||
ProcessLog(capsule);
|
ProcessLog(capsule);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void Verbose(string message, params object?[]? parameters)
|
||||||
|
{
|
||||||
|
Log(new LogCapsule()
|
||||||
|
{
|
||||||
|
Logger = this,
|
||||||
|
Message = message,
|
||||||
|
Parameters = parameters,
|
||||||
|
Severity = LogSeverity.Verbose,
|
||||||
|
TimeStamp = DateTime.Now
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public override void Trace(string message, params object?[]? parameters)
|
public override void Trace(string message, params object?[]? parameters)
|
||||||
{
|
{
|
||||||
Log(new LogCapsule()
|
Log(new LogCapsule()
|
||||||
|
|
|
@ -11,6 +11,7 @@ public abstract class LoggerBase(CallerInformation caller, ref Action<LogCapsule
|
||||||
|
|
||||||
internal readonly Action<LogCapsule> ProcessLog = logProcessorHandler;
|
internal readonly Action<LogCapsule> ProcessLog = logProcessorHandler;
|
||||||
|
|
||||||
|
public abstract void Verbose(string message, params object?[]? parameters);
|
||||||
public abstract void Trace(string message, params object?[]? parameters);
|
public abstract void Trace(string message, params object?[]? parameters);
|
||||||
public abstract void Debug(string message, params object?[]? parameters);
|
public abstract void Debug(string message, params object?[]? parameters);
|
||||||
public abstract void Information(string message, params object?[]? parameters);
|
public abstract void Information(string message, params object?[]? parameters);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user