Example #1
0
        /// <summary>
        /// Gets the extended log properties.
        /// </summary>
        /// <returns>The extended log properties string.</returns>
        private static string GetExtendedLogProperties()
        {
            var extendedProperties = new StringBuilder();

            try
            {
                var contextItems = LoggerCallContextItems.GetContextItems();
                if (contextItems != null && contextItems.Count != 0)
                {
                    foreach (DictionaryEntry entry in contextItems)
                    {
                        var itemValue = LoggerCallContextItems.GetContextItemValue(entry.Value);
                        extendedProperties.AppendFormat(CultureInfo.CurrentUICulture, " {0}: '{1}'. ", entry.Key, itemValue);
                    }
                }
            }
            catch (SecurityException)
            {
                // ignore the security exception - no item could have been set if we get the exception here.
            }
            catch (MethodAccessException)
            {
                // ignore the security exception - no item could have been set if we get the exception here.
            }

            return(extendedProperties.ToString());
        }
        public static string GetContextItem(object key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            var contextItems = LoggerCallContextItems.GetContextItems();

            return(contextItems == null || contextItems.Count == 0 || !contextItems.ContainsKey(key) ? null : LoggerCallContextItems.GetContextItemValue(contextItems[key]));
        }
Example #3
0
        public static void SetContextItem(object key, object value)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (value is string && !string.IsNullOrEmpty(value as string))
            {
                // remove any curly braces as the cause FormatException in WriteEvent method when args is passed to it.
                var stringValue = ((string)value).Replace("{", string.Empty).Replace("}", string.Empty);
                LoggerCallContextItems.SetContextItem(key, stringValue);
            }
            else
            {
                LoggerCallContextItems.SetContextItem(key, value);
            }
        }
Example #4
0
 public static void FlushContextItems()
 {
     LoggerCallContextItems.FlushContextItems();
 }
Example #5
0
 public static void ClearContextItem(object key)
 {
     LoggerCallContextItems.ClearContextItem(key);
 }