Пример #1
0
        /// <summary>
        /// Renders the specified MDC item and appends it to the specified <see cref="StringBuilder" />.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
        /// <param name="logEvent">Logging event.</param>
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            var tagologContext = MdcStorage.GetContext();

            if (string.IsNullOrEmpty(tagologContext))
            {
                return;
            }

            IDictionary <string, string> tags;
            IDictionary <string, string> builtInTags;

            TagSerializer.TagsFromString(tagologContext, out tags, out builtInTags);

            string itemValue;

            if (tags.ContainsKey(Item))
            {
                itemValue = tags[Item];
            }
            else if (builtInTags.ContainsKey(Item))
            {
                itemValue = builtInTags[Item];
            }
            else
            {
                return;
            }

            builder.Append(itemValue);
        }
Пример #2
0
        /// <summary>
        /// Renders all items from Tagolog MDC context and appends them to the specified <see cref="StringBuilder" />.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
        /// <param name="logEvent">Logging event.</param>
        protected override void Append( StringBuilder builder, LogEventInfo logEvent )
        {
            var tagologContext = MdcStorage.GetContext();

            if ( string.IsNullOrEmpty( tagologContext ) )
                return;

            IDictionary<string, string> tags;
            IDictionary<string, string> builtInTags;

            TagSerializer.TagsFromString( tagologContext, out tags, out builtInTags );

            var tagsAsList = tags.ToList();
            if ( BuiltInTags )
                tagsAsList.AddRange( builtInTags.ToList() );

            var orderByTags = ( OrderBy ) ?
                tagsAsList.OrderBy( _ => _.Key ).ToList() :
                tagsAsList.ToList();

            if ( 0 != orderByTags.Count )
                builder.Append( Prefix );

            for ( var i = 0 ; i < orderByTags.Count ; i ++ )
            {
                var tagPair = orderByTags[ i ];
                builder.Append( TagToString( tagPair.Key, tagPair.Value ) );

                if ( i < orderByTags.Count - 1 )
                {
                    if ( !string.IsNullOrEmpty( Separator ) )
                        builder.Append( Separator );
                }
            }
        }