|
| 1 | +// Copyright 2019 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 Serilog.Events; |
| 16 | +using System; |
| 17 | +using System.Collections; |
| 18 | +using System.Collections.Generic; |
| 19 | + |
| 20 | +namespace Serilog.Extensions.Logging |
| 21 | +{ |
| 22 | + readonly struct SerilogLogValues : IReadOnlyList<KeyValuePair<string, object>> |
| 23 | + { |
| 24 | + // Note, this struct is only used in a very limited context internally, so we ignore |
| 25 | + // the possibility of fields being null via the default struct initialization. |
| 26 | + |
| 27 | + private readonly MessageTemplate _messageTemplate; |
| 28 | + private readonly IReadOnlyDictionary<string, LogEventPropertyValue> _properties; |
| 29 | + private readonly KeyValuePair<string, object>[] _values; |
| 30 | + |
| 31 | + public SerilogLogValues(MessageTemplate messageTemplate, IReadOnlyDictionary<string, LogEventPropertyValue> properties) |
| 32 | + { |
| 33 | + _messageTemplate = messageTemplate ?? throw new ArgumentNullException(nameof(messageTemplate)); |
| 34 | + |
| 35 | + // The dictionary is needed for rendering through the message template |
| 36 | + _properties = properties ?? throw new ArgumentNullException(nameof(properties)); |
| 37 | + |
| 38 | + // The array is needed because the IReadOnlyList<T> interface expects indexed access |
| 39 | + _values = new KeyValuePair<string, object>[_properties.Count + 1]; |
| 40 | + var i = 0; |
| 41 | + foreach (var p in properties) |
| 42 | + { |
| 43 | + _values[i] = new KeyValuePair<string, object>(p.Key, (p.Value is ScalarValue sv) ? sv.Value : p.Value); |
| 44 | + ++i; |
| 45 | + } |
| 46 | + _values[i] = new KeyValuePair<string, object>("{OriginalFormat}", _messageTemplate.Text); |
| 47 | + } |
| 48 | + |
| 49 | + public KeyValuePair<string, object> this[int index] |
| 50 | + { |
| 51 | + get => _values[index]; |
| 52 | + } |
| 53 | + |
| 54 | + public int Count => _properties.Count + 1; |
| 55 | + |
| 56 | + public IEnumerator<KeyValuePair<string, object>> GetEnumerator() => ((IEnumerable<KeyValuePair<string, object>>)_values).GetEnumerator(); |
| 57 | + |
| 58 | + public override string ToString() => _messageTemplate.Render(_properties); |
| 59 | + |
| 60 | + IEnumerator IEnumerable.GetEnumerator() => _values.GetEnumerator(); |
| 61 | + } |
| 62 | +} |
0 commit comments