|
| 1 | +// Copyright © Serilog Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System.Diagnostics; |
| 16 | +using Serilog.Events; |
| 17 | + |
| 18 | +namespace Serilog.Expressions.Runtime.Support; |
| 19 | + |
| 20 | +/// <summary> |
| 21 | +/// Nest (un-flatten) properties with dotted names. A property with name <c>"a.b"</c> will be transmitted to Seq as |
| 22 | +/// a structure with name <c>"a"</c>, and one member <c>"b"</c>. |
| 23 | +/// </summary> |
| 24 | +/// <remarks>Forked from <a href="https://github.com/datalust/serilog-sinks-seq/blob/6815d7307b477747e05e782f7dfbcff8c8dd20a2/src/Serilog.Sinks.Seq/Sinks/Seq/Conventions/UnflattenDottedPropertyNames.cs"/>.</remarks> |
| 25 | +static class UnflattenDottedPropertyNames |
| 26 | +{ |
| 27 | + const int MaxDepth = 10; |
| 28 | + |
| 29 | + public static IReadOnlyDictionary<string, LogEventPropertyValue> ProcessDottedPropertyNames(IReadOnlyDictionary<string, LogEventPropertyValue> maybeDotted) |
| 30 | + { |
| 31 | + return DottedToNestedRecursive(maybeDotted, 0); |
| 32 | + } |
| 33 | + |
| 34 | + static IReadOnlyDictionary<string, LogEventPropertyValue> DottedToNestedRecursive(IReadOnlyDictionary<string, LogEventPropertyValue> maybeDotted, int depth) |
| 35 | + { |
| 36 | + if (depth == MaxDepth) |
| 37 | + return maybeDotted; |
| 38 | + |
| 39 | + // Assume that the majority of entries will be bare or have unique prefixes. |
| 40 | + var result = new Dictionary<string, LogEventPropertyValue>(maybeDotted.Count); |
| 41 | + |
| 42 | + // Sorted for determinism. |
| 43 | + var dotted = new SortedDictionary<string, LogEventPropertyValue>(StringComparer.Ordinal); |
| 44 | + |
| 45 | + // First - give priority to bare names, since these would otherwise be claimed by the parents of further nested |
| 46 | + // layers and we'd have nowhere to put them when resolving conflicts. (Dotted entries that conflict can keep their dotted keys). |
| 47 | + |
| 48 | + foreach (var kv in maybeDotted) |
| 49 | + { |
| 50 | + if (IsDottedIdentifier(kv.Key)) |
| 51 | + { |
| 52 | + // Stash for processing in the next stage. |
| 53 | + dotted.Add(kv.Key, kv.Value); |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + result.Add(kv.Key, kv.Value); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Then - for dotted keys with a prefix not already present in the result, convert to structured data and add to |
| 62 | + // the result. Any set of dotted names that collide with a preexisting key will be left as-is. |
| 63 | + |
| 64 | + string? prefix = null; |
| 65 | + Dictionary<string, LogEventPropertyValue>? nested = null; |
| 66 | + foreach (var kv in dotted) |
| 67 | + { |
| 68 | + var (newPrefix, rem) = TakeFirstIdentifier(kv.Key); |
| 69 | + |
| 70 | + if (prefix != null && prefix != newPrefix) |
| 71 | + { |
| 72 | + result.Add(prefix, MakeStructureValue(DottedToNestedRecursive(nested!, depth + 1))); |
| 73 | + prefix = null; |
| 74 | + nested = null; |
| 75 | + } |
| 76 | + |
| 77 | + if (nested != null && !nested.ContainsKey(rem)) |
| 78 | + { |
| 79 | + prefix = newPrefix; |
| 80 | + nested.Add(rem, kv.Value); |
| 81 | + } |
| 82 | + else if (nested == null && !result.ContainsKey(newPrefix)) |
| 83 | + { |
| 84 | + prefix = newPrefix; |
| 85 | + nested = new () { { rem, kv.Value } }; |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + result.Add(kv.Key, kv.Value); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (prefix != null) |
| 94 | + { |
| 95 | + result[prefix] = MakeStructureValue(DottedToNestedRecursive(nested!, depth + 1)); |
| 96 | + } |
| 97 | + |
| 98 | + return result; |
| 99 | + } |
| 100 | + |
| 101 | + static StructureValue MakeStructureValue(IReadOnlyDictionary<string,LogEventPropertyValue> properties) |
| 102 | + { |
| 103 | + return new StructureValue(properties.Select(kv => new LogEventProperty(kv.Key, kv.Value)), typeTag: null); |
| 104 | + } |
| 105 | + |
| 106 | + static bool IsDottedIdentifier(string key) => |
| 107 | + key.Contains('.') && |
| 108 | + !key.StartsWith(".", StringComparison.Ordinal) && |
| 109 | + !key.EndsWith(".", StringComparison.Ordinal) && |
| 110 | + key.Split('.').All(IsIdentifier); |
| 111 | + |
| 112 | + static bool IsIdentifier(string s) => s.Length != 0 && |
| 113 | + !char.IsDigit(s[0]) && |
| 114 | + s.All(ch => char.IsLetter(ch) || char.IsDigit(ch) || ch == '_'); |
| 115 | + |
| 116 | + static (string, string) TakeFirstIdentifier(string dottedIdentifier) |
| 117 | + { |
| 118 | + // We can do this simplistically because keys in `dotted` conform to `IsDottedName`. |
| 119 | + Debug.Assert(IsDottedIdentifier(dottedIdentifier)); |
| 120 | + |
| 121 | + var firstDot = dottedIdentifier.IndexOf('.'); |
| 122 | + var prefix = dottedIdentifier.Substring(0, firstDot); |
| 123 | + var rem = dottedIdentifier.Substring(firstDot + 1); |
| 124 | + return (prefix, rem); |
| 125 | + } |
| 126 | +} |
0 commit comments