/
PropertyValueConverter.cs
410 lines (349 loc) · 14.6 KB
/
PropertyValueConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright 2013-2021 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace Serilog.Capturing;
// Values in Serilog are simplified down into a lowest-common-denominator internal
// type system so that there is a better chance of code written with one sink in
// mind working correctly with any other. This technique also makes the programmer
// writing a log event (roughly) in control of the cost of recording that event.
partial class PropertyValueConverter : ILogEventPropertyFactory, ILogEventPropertyValueFactory
{
static readonly HashSet<Type> BuiltInScalarTypes = new()
{
typeof(bool),
typeof(char),
typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint),
typeof(long), typeof(ulong), typeof(float), typeof(double), typeof(decimal),
typeof(string),
typeof(DateTime), typeof(DateTimeOffset), typeof(TimeSpan),
typeof(Guid), typeof(Uri),
#if FEATURE_DATE_AND_TIME_ONLY
typeof(TimeOnly), typeof(DateOnly)
#endif
};
readonly IDestructuringPolicy[] _destructuringPolicies;
readonly IScalarConversionPolicy[] _scalarConversionPolicies;
readonly DepthLimiter _depthLimiter;
readonly int _maximumStringLength;
readonly int _maximumCollectionCount;
readonly bool _propagateExceptions;
public PropertyValueConverter(
int maximumDestructuringDepth,
int maximumStringLength,
int maximumCollectionCount,
IEnumerable<Type> additionalScalarTypes,
IEnumerable<IDestructuringPolicy> additionalDestructuringPolicies,
bool propagateExceptions)
{
Guard.AgainstNull(additionalScalarTypes);
Guard.AgainstNull(additionalDestructuringPolicies);
if (maximumDestructuringDepth < 0) throw new ArgumentOutOfRangeException(nameof(maximumDestructuringDepth));
if (maximumStringLength < 2) throw new ArgumentOutOfRangeException(nameof(maximumStringLength));
if (maximumCollectionCount < 1) throw new ArgumentOutOfRangeException(nameof(maximumCollectionCount));
_propagateExceptions = propagateExceptions;
_maximumStringLength = maximumStringLength;
_maximumCollectionCount = maximumCollectionCount;
_scalarConversionPolicies = new IScalarConversionPolicy[]
{
new SimpleScalarConversionPolicy(BuiltInScalarTypes.Concat(additionalScalarTypes)),
new EnumScalarConversionPolicy(),
new ByteArrayScalarConversionPolicy(),
#if FEATURE_SPAN
new ByteMemoryScalarConversionPolicy(),
#endif
};
_destructuringPolicies = additionalDestructuringPolicies
.Concat(new IDestructuringPolicy[]
{
new DelegateDestructuringPolicy(),
new ReflectionTypesScalarDestructuringPolicy()
})
.ToArray();
_depthLimiter = new(maximumDestructuringDepth, this);
}
public LogEventProperty CreateProperty(string name, object? value, bool destructureObjects = false)
{
return new(name, CreatePropertyValue(value, destructureObjects));
}
public LogEventPropertyValue CreatePropertyValue(object? value, bool destructureObjects = false)
{
return CreatePropertyValue(value, destructureObjects, 1);
}
public LogEventPropertyValue CreatePropertyValue(object? value, Destructuring destructuring)
{
try
{
return CreatePropertyValue(value, destructuring, 1);
}
catch (Exception ex)
{
SelfLog.WriteLine("Exception caught while converting property value: {0}", ex);
if (_propagateExceptions)
throw;
return new ScalarValue("Capturing the property value threw an exception: " + ex.GetType().Name);
}
}
LogEventPropertyValue CreatePropertyValue(object? value, bool destructureObjects, int depth)
{
return CreatePropertyValue(
value,
destructureObjects ?
Destructuring.Destructure :
Destructuring.Default,
depth);
}
LogEventPropertyValue CreatePropertyValue(object? value, Destructuring destructuring, int depth)
{
if (value == null)
return new ScalarValue(null);
if (destructuring == Destructuring.Stringify)
{
return Stringify(value);
}
if (destructuring == Destructuring.Destructure)
{
if (value is string stringValue)
{
value = TruncateIfNecessary(stringValue);
}
}
if (value is string)
return new ScalarValue(value);
foreach (var scalarConversionPolicy in _scalarConversionPolicies)
{
if (scalarConversionPolicy.TryConvertToScalar(value, out var converted))
return converted;
}
DepthLimiter.SetCurrentDepth(depth);
if (destructuring == Destructuring.Destructure)
{
foreach (var destructuringPolicy in _destructuringPolicies)
{
if (destructuringPolicy.TryDestructure(value, _depthLimiter, out var result))
return result;
}
}
if (TryConvertEnumerable(value, destructuring, out var enumerableResult))
return enumerableResult;
if (TryConvertValueTuple(value, destructuring, out var tupleResult))
return tupleResult;
if (TryConvertCompilerGeneratedType(value, destructuring, out var compilerGeneratedResult))
return compilerGeneratedResult;
return new ScalarValue(value.ToString() ?? "");
}
bool TryConvertEnumerable(object value, Destructuring destructuring, [NotNullWhen(true)] out LogEventPropertyValue? result)
{
var valueType = value.GetType();
if (value is IEnumerable enumerable)
{
// Only dictionaries with 'scalar' keys are permitted, as
// more complex keys may not serialize to unique values for
// representation in sinks. This check strengthens the expectation
// that resulting dictionary is representable in JSON as well
// as richer formats (e.g. XML, .NET type-aware...).
// Only actual dictionaries are supported, as arbitrary types
// can implement multiple IDictionary interfaces and thus introduce
// multiple different interpretations.
if (TryGetDictionary(value, valueType, out var dictionary))
{
result = new DictionaryValue(MapToDictionaryElements(dictionary, destructuring));
return true;
IEnumerable<KeyValuePair<ScalarValue, LogEventPropertyValue>> MapToDictionaryElements(IDictionary dictionaryEntries, Destructuring destructure)
{
var count = 0;
foreach (DictionaryEntry entry in dictionaryEntries)
{
if (++count > _maximumCollectionCount)
{
yield break;
}
var pair = new KeyValuePair<ScalarValue, LogEventPropertyValue>(
(ScalarValue)_depthLimiter.CreatePropertyValue(entry.Key, destructure),
_depthLimiter.CreatePropertyValue(entry.Value, destructure));
if (pair.Key.Value != null)
yield return pair;
}
}
}
result = new SequenceValue(MapToSequenceElements(enumerable, destructuring));
return true;
IEnumerable<LogEventPropertyValue> MapToSequenceElements(IEnumerable sequence, Destructuring destructure)
{
var count = 0;
foreach (var element in sequence)
{
if (++count > _maximumCollectionCount)
{
yield break;
}
yield return _depthLimiter.CreatePropertyValue(element, destructure);
}
}
}
result = null;
return false;
}
#if FEATURE_ITUPLE
bool TryConvertValueTuple(object value, Destructuring destructuring, [NotNullWhen(true)] out LogEventPropertyValue? result)
{
if (value is not ITuple tuple)
{
result = null;
return false;
}
var elements = new List<LogEventPropertyValue>();
for (var i = 0; i < tuple.Length; i++)
{
var fieldValue = tuple[i];
var propertyValue = _depthLimiter.CreatePropertyValue(fieldValue, destructuring);
elements.Add(propertyValue);
}
result = new SequenceValue(elements);
return true;
}
#else
bool TryConvertValueTuple(object value, Destructuring destructuring, [NotNullWhen(true)] out LogEventPropertyValue? result)
{
var valueType = value.GetType();
if (!(value is IStructuralEquatable && valueType.IsConstructedGenericType))
{
result = null;
return false;
}
var definition = valueType.GetGenericTypeDefinition();
// Ignore the 8+ value case for now.
#if FEATURE_VALUETUPLE
if (definition == typeof(ValueTuple<>) || definition == typeof(ValueTuple<,>) ||
definition == typeof(ValueTuple<,,>) || definition == typeof(ValueTuple<,,,>) ||
definition == typeof(ValueTuple<,,,,>) || definition == typeof(ValueTuple<,,,,,>) ||
definition == typeof(ValueTuple<,,,,,,>))
#else
// ReSharper disable once PossibleNullReferenceException
if (definition.FullName is
"System.ValueTuple`1" or
"System.ValueTuple`2" or
"System.ValueTuple`3" or
"System.ValueTuple`4" or
"System.ValueTuple`5" or
"System.ValueTuple`6" or
"System.ValueTuple`7")
#endif
{
var elements = new List<LogEventPropertyValue>();
foreach (var field in valueType.GetTypeInfo().DeclaredFields)
{
if (field.IsPublic && !field.IsStatic)
{
var fieldValue = field.GetValue(value);
var propertyValue = _depthLimiter.CreatePropertyValue(fieldValue, destructuring);
elements.Add(propertyValue);
}
}
result = new SequenceValue(elements);
return true;
}
result = null;
return false;
}
#endif
bool TryConvertCompilerGeneratedType(object value, Destructuring destructuring, [NotNullWhen(true)] out LogEventPropertyValue? result)
{
var valueType = value.GetType();
if (destructuring == Destructuring.Destructure)
{
var typeTag = valueType.Name;
if (typeTag.Length <= 0 || IsCompilerGeneratedType(valueType))
{
typeTag = null;
}
result = new StructureValue(GetProperties(value), typeTag);
return true;
}
result = null;
return false;
}
LogEventPropertyValue Stringify(object value)
{
var stringified = value.ToString();
var truncated = stringified == null ? "" : TruncateIfNecessary(stringified);
return new ScalarValue(truncated);
}
string TruncateIfNecessary(string text)
{
if (text.Length > _maximumStringLength)
{
return text.Substring(0, _maximumStringLength - 1) + "…";
}
return text;
}
static bool TryGetDictionary(object value, Type valueType, [NotNullWhen(true)] out IDictionary? dictionary)
{
if (valueType.IsConstructedGenericType &&
valueType.GetGenericTypeDefinition() == typeof(Dictionary<,>) &&
IsValidDictionaryKeyType(valueType.GenericTypeArguments[0]))
{
dictionary = (IDictionary)value;
return true;
}
dictionary = null;
return false;
}
static bool IsValidDictionaryKeyType(Type valueType)
{
return BuiltInScalarTypes.Contains(valueType) ||
valueType.GetTypeInfo().IsEnum;
}
IEnumerable<LogEventProperty> GetProperties(object value)
{
foreach (var prop in value.GetType().GetPropertiesRecursive())
{
object propValue;
try
{
propValue = prop.GetValue(value)!;
}
catch (TargetParameterCountException)
{
// These properties would ideally be ignored; since they never produce values they're not
// of concern to auditing and exceptions can be suppressed.
SelfLog.WriteLine("The property accessor {0} is a non-default indexer", prop);
continue;
}
catch (TargetInvocationException ex)
{
SelfLog.WriteLine("The property accessor {0} threw exception: {1}", prop, ex);
if (_propagateExceptions)
throw;
propValue = "The property accessor threw an exception: " + ex.InnerException?.GetType().Name;
}
catch (NotSupportedException)
{
SelfLog.WriteLine("The property accessor {0} is not supported via Reflection API", prop);
if (_propagateExceptions)
throw;
propValue = "Accessing this property is not supported via Reflection API";
}
yield return new(prop.Name, _depthLimiter.CreatePropertyValue(propValue, Destructuring.Destructure));
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsCompilerGeneratedType(Type type)
{
var typeInfo = type.GetTypeInfo();
var typeName = type.Name;
// C# Anonymous types always start with "<>" and VB's start with "VB$"
return typeInfo.IsGenericType && typeInfo.IsSealed && type.Namespace == null
&& (typeName[0] == '<'
|| (typeName.Length > 2 && typeName[0] == 'V' && typeName[1] == 'B' && typeName[2] == '$'));
}
}