/// <summary>Clears the content of current logical context.</summary> /// <param name="free">Free the full slot.</param> public static void Clear(bool free) { if (free) { MappedDiagnosticsLogicalContext.SetThreadLocal((IDictionary <string, object>)null); } else { MappedDiagnosticsLogicalContext.GetLogicalThreadDictionary(true).Clear(); } }
/// <summary> /// Simulate ImmutableDictionary behavior (which is not yet part of all .NET frameworks). /// In future the real ImmutableDictionary could be used here to minimize memory usage and copying time. /// </summary> /// <param name="clone">Must be true for any subsequent dictionary modification operation</param> /// <returns></returns> private static IDictionary <string, object> GetLogicalThreadDictionary(bool clone = false) { IDictionary <string, object> newValue = MappedDiagnosticsLogicalContext.GetThreadLocal(); if (newValue == null) { if (!clone) { return(MappedDiagnosticsLogicalContext.EmptyDefaultDictionary); } newValue = (IDictionary <string, object>) new Dictionary <string, object>(); MappedDiagnosticsLogicalContext.SetThreadLocal(newValue); } else if (clone) { newValue = (IDictionary <string, object>) new Dictionary <string, object>(newValue); MappedDiagnosticsLogicalContext.SetThreadLocal(newValue); } return(newValue); }