Skip to content

Commit 8551dbf

Browse files
committed
chore: add code to exclude console logger when using testadapter
1 parent a3772bf commit 8551dbf

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/BenchmarkDotNet.TestAdapter/BenchmarkExecutor.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using BenchmarkDotNet.Configs;
2+
using BenchmarkDotNet.Loggers;
23
using BenchmarkDotNet.Running;
34
using BenchmarkDotNet.TestAdapter.Remoting;
45
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
56
using System;
67
using System.Collections.Generic;
8+
using System.Collections.Immutable;
79
using System.Linq;
10+
using System.Reflection;
811
using System.Threading;
912

1013
namespace BenchmarkDotNet.TestAdapter
@@ -14,6 +17,9 @@ namespace BenchmarkDotNet.TestAdapter
1417
/// </summary>
1518
internal class BenchmarkExecutor
1619
{
20+
// Gets FieldInfo of ImmutableConfig's loggers.
21+
private static readonly FieldInfo? loggersField = typeof(ImmutableConfig).GetField("loggers", BindingFlags.Instance | BindingFlags.NonPublic);
22+
1723
private readonly CancellationTokenSource cts = new ();
1824

1925
/// <summary>
@@ -64,10 +70,21 @@ public void RunBenchmarks(string assemblyPath, TestExecutionRecorderWrapper reco
6470

6571
// Modify all the benchmarks so that the event process and logger is added.
6672
benchmarks = benchmarks
67-
.Select(b => new BenchmarkRunInfo(
68-
b.BenchmarksCases,
69-
b.Type,
70-
b.Config.AddEventProcessor(eventProcessor).AddLogger(logger).CreateImmutableConfig()))
73+
.Select(b =>
74+
{
75+
ImmutableConfig config = b.Config.AddEventProcessor(eventProcessor).AddLogger(logger).CreateImmutableConfig();
76+
77+
// Remove console logger from ImmutableCofig to fix duplicated console logs are outputted issue.
78+
if (loggersField != null && loggersField.DeclaringType == typeof(ImmutableHashSet))
79+
{
80+
var loggers = config.GetLoggers()
81+
.Where(x => x is not ConsoleLogger)
82+
.ToImmutableHashSet();
83+
loggersField.SetValue(config, loggers);
84+
}
85+
86+
return new BenchmarkRunInfo(b.BenchmarksCases, b.Type, config);
87+
})
7188
.ToArray();
7289

7390
// Run all the benchmarks, and ensure that any tests that don't have a result yet are sent.

0 commit comments

Comments
 (0)