108 lines
2.8 KiB
C#
Executable File
108 lines
2.8 KiB
C#
Executable File
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Opsoft.NetCore.O3App.Shared
|
|
{
|
|
public class LogDictionary : IDictionary<string, object>
|
|
{
|
|
Dictionary<string, object> logExtraInfo = new Dictionary<string, object>();
|
|
Dictionary<string, int> keyCounter = new Dictionary<string, int>();
|
|
|
|
public object this[string key]
|
|
{
|
|
get
|
|
{
|
|
Guard.ArgumentNotNullOrWhiteSpace(key, nameof(key));
|
|
if (!logExtraInfo.ContainsKey(key))
|
|
{
|
|
throw new KeyNotFoundException($"key dost not exist. key: {key}");
|
|
}
|
|
|
|
return logExtraInfo[key];
|
|
}
|
|
set
|
|
{
|
|
logExtraInfo[key] = Guard.ArgumentNotNull(value, "value");
|
|
}
|
|
}
|
|
|
|
public ICollection<string> Keys => logExtraInfo.Keys;
|
|
|
|
public ICollection<object> Values => logExtraInfo.Values;
|
|
|
|
public int Count => logExtraInfo.Count;
|
|
|
|
public bool IsReadOnly => true;
|
|
|
|
public void Add(string key, object value)
|
|
{
|
|
logExtraInfo.Add(GetUniqueKey(key), value);
|
|
}
|
|
|
|
public void Add(KeyValuePair<string, object> item)
|
|
{
|
|
logExtraInfo.Add(GetUniqueKey(item.Key), item.Value);
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
logExtraInfo.Clear();
|
|
}
|
|
|
|
public bool Contains(KeyValuePair<string, object> item)
|
|
{
|
|
return logExtraInfo.ContainsKey(item.Key) && logExtraInfo[item.Key] == item.Value;
|
|
}
|
|
|
|
public bool ContainsKey(string key)
|
|
{
|
|
return logExtraInfo.ContainsKey(key);
|
|
}
|
|
|
|
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
|
|
{
|
|
return logExtraInfo.GetEnumerator();
|
|
}
|
|
|
|
public bool Remove(string key)
|
|
{
|
|
return logExtraInfo.Remove(key);
|
|
}
|
|
|
|
public bool Remove(KeyValuePair<string, object> item)
|
|
{
|
|
return logExtraInfo.Remove(item.Key);
|
|
}
|
|
|
|
public bool TryGetValue(string key, out object value)
|
|
{
|
|
return logExtraInfo.TryGetValue(key, out value);
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return logExtraInfo.GetEnumerator();
|
|
}
|
|
|
|
private string GetUniqueKey(string key)
|
|
{
|
|
if (keyCounter.TryGetValue(key, out int count))
|
|
{
|
|
keyCounter[key] = ++count;
|
|
}
|
|
else
|
|
{
|
|
keyCounter[key] = count;
|
|
}
|
|
|
|
return count > 0 ? $"{key}{count}" : key;
|
|
}
|
|
}
|
|
}
|