<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0-preview.6.24327.7" />

FormattedLogValues

struct FormattedLogValues : IReadOnlyList<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>>, IEnumerable, IReadOnlyCollection<KeyValuePair<string, object>>
using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Threading; namespace Microsoft.Extensions.Logging { [System.Runtime.CompilerServices.NullableContext(2)] [System.Runtime.CompilerServices.Nullable(0)] internal readonly struct FormattedLogValues : IReadOnlyList<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>>, IEnumerable, IReadOnlyCollection<KeyValuePair<string, object>> { internal const int MaxCachedFormatters = 1024; private const string NullFormat = "[null]"; private static int s_count; private static readonly ConcurrentDictionary<string, LogValuesFormatter> s_formatters = new ConcurrentDictionary<string, LogValuesFormatter>(); private readonly LogValuesFormatter _formatter; private readonly object[] _values; private readonly string _originalMessage; internal LogValuesFormatter Formatter => _formatter; [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1, 2 })] public KeyValuePair<string, object> this[int index] { [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1, 2 })] get { if (index < 0 || index >= Count) throw new IndexOutOfRangeException("index"); if (index == Count - 1) return new KeyValuePair<string, object>("{OriginalFormat}", _originalMessage); return _formatter.GetValue(_values, index); } } public int Count { get { if (_formatter == null) return 1; return _formatter.ValueNames.Count + 1; } } public FormattedLogValues(string format, params object[] values) { if (values != null && values.Length != 0 && format != null) { if (s_count >= 1024) { if (!s_formatters.TryGetValue(format, out _formatter)) _formatter = new LogValuesFormatter(format); } else _formatter = s_formatters.GetOrAdd(format, delegate(string f) { Interlocked.Increment(ref s_count); return new LogValuesFormatter(f); }); } else _formatter = null; _originalMessage = (format ?? "[null]"); _values = values; } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1, 2 })] public IEnumerator<KeyValuePair<string, object>> GetEnumerator() { int num; for (int i = 0; i < Count; i = num) { yield return this[i]; num = i + 1; } } [System.Runtime.CompilerServices.NullableContext(1)] public override string ToString() { if (_formatter == null) return _originalMessage; return _formatter.Format(_values); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } }