using System; using System.Collections; using System.Collections.Generic; namespace Opsoft.NetCore.O3App.Shared { public class LogDictionary : IDictionary { Dictionary logExtraInfo = new Dictionary(); Dictionary keyCounter = new Dictionary(); 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 Keys => logExtraInfo.Keys; public ICollection 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 item) { logExtraInfo.Add(GetUniqueKey(item.Key), item.Value); } public void Clear() { logExtraInfo.Clear(); } public bool Contains(KeyValuePair item) { return logExtraInfo.ContainsKey(item.Key) && logExtraInfo[item.Key] == item.Value; } public bool ContainsKey(string key) { return logExtraInfo.ContainsKey(key); } public void CopyTo(KeyValuePair[] array, int arrayIndex) { throw new NotImplementedException(); } public IEnumerator> GetEnumerator() { return logExtraInfo.GetEnumerator(); } public bool Remove(string key) { return logExtraInfo.Remove(key); } public bool Remove(KeyValuePair 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; } } }