From 47a2348a9ba26eb6bf5fed190dadaab720a88fde Mon Sep 17 00:00:00 2001 From: Justin Anderson Date: Wed, 13 Jan 2021 21:19:49 -0800 Subject: [PATCH] Update log scope to be more compatible with JSON console logger. --- .../KeyValueLogScope.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.Diagnostics.Monitoring.RestServer/KeyValueLogScope.cs b/src/Microsoft.Diagnostics.Monitoring.RestServer/KeyValueLogScope.cs index 2fe1c55c03..9e689637db 100644 --- a/src/Microsoft.Diagnostics.Monitoring.RestServer/KeyValueLogScope.cs +++ b/src/Microsoft.Diagnostics.Monitoring.RestServer/KeyValueLogScope.cs @@ -9,12 +9,15 @@ namespace Microsoft.Diagnostics.Monitoring.RestServer { // Logger implementations have different ways of serializing log scopes. This class helps those loggers - // serialize the scope information in the best way possible for each of the implementations. For example, - // the console logger will only call ToString on the scope data, thus the data needs to be formatted appropriately - // in the ToString method. Another example, the event log logger will check if the scope data impelements - // IEnumerable> and then formats each value from the enumeration; it will fallback - // calling the ToString method otherwise. - internal class KeyValueLogScope : IEnumerable> + // serialize the scope information in the best way possible for each of the implementations. + // + // Handled examples: + // - Simple Console Logger: only calls ToString, thus the data needs to be formatted in the ToString method. + // - JSON Console Logger: checks for IReadOnlyCollection> and formats each value + // in the enumeration; otherwise falls back to ToString. + // - Event Log Logger: checks for IEnumerable> and formats each value + // in the enumeration; otherwise falls back to ToString. + internal class KeyValueLogScope : IReadOnlyCollection> { public IDictionary Values = new Dictionary(); @@ -29,6 +32,8 @@ IEnumerator IEnumerable.GetEnumerator() return ((IEnumerable)Values).GetEnumerator(); } + int IReadOnlyCollection>.Count => Values.Count; + public override string ToString() { StringBuilder builder = new StringBuilder();