/// <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 ); } } }
/// <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); }
// Next release /// <summary> /// Gets or sets a value used as a name of Json container to append all Tagolog tags into. /// If no container name specified, tags appednded to the root container. /// </summary> /// <example> /// TagsContainerName = "" /// { ... , TagologTag1=Value1, TagologTag2=Value1, ... } /// /// TagsContainerName = "TagologSeparateContainer" /// { ... TagologSeparateContainer={ TagologTag1=Value1, TagologTag2=Value1, ... } } /// </example> // [DefaultParameter] // public string TagsContainerName { get; set; } /// <summary> /// Formats the log event as a JSON document for writing. /// </summary> /// <param name="logEvent">The log event to be formatted.</param> /// <returns>A JSON string representation of the log event.</returns> protected override string GetFormattedMessage(LogEventInfo logEvent) { var jsonLayoutResult = "{}"; if (0 != Attributes.Count) { // "{ attr1:value1, attr2:value2, ... attrN:valueN }" jsonLayoutResult = base.GetFormattedMessage(logEvent); } var tags = MdcStorage.GetTags(builtInTagsEnabled: BuiltInTagsEnabled, orderByAsc: null); if (null == tags) { return(jsonLayoutResult); } var stringBuilder = new StringBuilder(jsonLayoutResult); using (var stringWriter = new StringWriter(stringBuilder)) { using (var jsonWriter = new JsonTextWriter(stringWriter)) { jsonWriter.Formatting = Formatting.None; jsonWriter.WriteStartObject(); for (int i = 0; i < tags.Length; i++) { var tagKey = tags[i].Key; if (!string.IsNullOrEmpty(TagsKeyPrefix)) { tagKey = TagsKeyPrefix + tagKey; } jsonWriter.WritePropertyName(tagKey); jsonWriter.WriteValue(tags[i].Value); } jsonWriter.WriteEndObject(); } } // At this point, string builder contains two Json fragments. // "{ attr1:value1, attr2:value2, ... attrN:valueN }{ tag1:value1, tag2:value2, ... tagN:valueN }" // Let's join them! stringBuilder.Remove(jsonLayoutResult.Length - 1, 2); if (0 != Attributes.Count) { stringBuilder.Insert(jsonLayoutResult.Length - 1, (SuppressSpaces) ? "," : ", "); } return(stringBuilder.ToString()); }