Fix metrics snapshot to prevent modified collection exception. (#1444)
authorJustin Anderson <jander-msft@users.noreply.github.com>
Mon, 17 Aug 2020 21:20:28 +0000 (14:20 -0700)
committerGitHub <noreply@github.com>
Mon, 17 Aug 2020 21:20:28 +0000 (14:20 -0700)
src/Microsoft.Diagnostics.Monitoring/Metrics/MetricsStore.cs

index 756beb667b0cb74f0c78571e90076086ea574041..70008dc47871e339bc36d634f35ec045a6b29447 100644 (file)
@@ -97,7 +97,11 @@ namespace Microsoft.Diagnostics.Monitoring
             Dictionary<MetricKey, Queue<Metric>> copy = null;
             lock (_allMetrics)
             {
-                copy = new Dictionary<MetricKey, Queue<Metric>>(_allMetrics);
+                copy = new Dictionary<MetricKey, Queue<Metric>>();
+                foreach (var metricGroup in _allMetrics)
+                {
+                    copy.Add(metricGroup.Key, new Queue<Metric>(metricGroup.Value));
+                }
             }
 
             using var writer = new StreamWriter(outputStream, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false), bufferSize: 1024, leaveOpen: true);