/// <summary> /// Sets a property name value pair in the desired logging context /// </summary> /// <param name="level">Defines the context level (global / thread) for the property storage</param> /// <param name="propertyName">The desired property name</param> /// <param name="propertyValue">The desired property value</param> /// <returns>The current property value for the specified context name, or null if not set</returns> public static object SetContext(ContextLevel level, string propertyName, object propertyValue) { object retValue = (level == ContextLevel.Global) ? GlobalContext.Properties[propertyName] : ThreadContext.Properties[propertyName]; // Check the context level if (level == ContextLevel.Global) { // Set global context GlobalContext.Properties[propertyName] = propertyValue; } else { // Set the thread context ThreadContext.Properties[propertyName] = propertyValue; } // Return previous value for the context name (or null) return retValue; }
/// <summary> /// Clears a property name value pair from the desired logging context /// </summary> /// <param name="level">Defines the context level (global / thread) for the property storage</param> /// <param name="propertyName">The desired property name</param> /// <returns>The current property value for the specified context name, or null if not set</returns> public static object ClearContext(ContextLevel level, string propertyName) { object retValue = (level == ContextLevel.Global) ? GlobalContext.Properties[propertyName] : ThreadContext.Properties[propertyName]; // Check the context level if (level == ContextLevel.Global) { // Remove global context GlobalContext.Properties.Remove(propertyName); } else { // Remove thread context ThreadContext.Properties.Remove(propertyName); } // Return previous value for the context name (or null) return retValue; }