Skip to content

Commit 2491959

Browse files
Mohammad DehghanMohammad Dehghan
authored andcommitted
Add logging to recording controllers
1 parent bfa4fd6 commit 2491959

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

SG.CodeCoverage.Tests.NetFx/InstrumenterTester.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,24 @@ public class InstrumenterTester
2020
public string MapFilePath { get; }
2121
public InstrumentationMap Map { get; private set; }
2222
public string InstrumentedAssemblyPath { get; private set; }
23+
private readonly ILogger _logger;
2324

2425
static InstrumenterTester()
2526
{
2627
DefaultOutputPath = Path.Combine(Path.GetTempPath(), "SG.CodeCoverage");
2728
}
2829

29-
public InstrumenterTester()
30+
public InstrumenterTester(ILogger logger = null)
3031
{
3132
OutputPath = DefaultOutputPath;
33+
_logger = logger ?? new ConsoleLogger();
3234
}
3335

34-
public InstrumenterTester(string existingInstrumentedSampleFolder)
36+
public InstrumenterTester(string existingInstrumentedSampleFolder, ILogger logger = null)
3537
{
3638
OutputPath = existingInstrumentedSampleFolder;
3739
InstrumentedAssemblyPath = Path.Combine(OutputPath, Path.GetFileName(typeof(PrimeCalculator).Assembly.Location));
40+
_logger = logger ?? new ConsoleLogger();
3841
}
3942

4043
public void InstrumentSampleProject()
@@ -55,7 +58,7 @@ public void InstrumentSampleProject()
5558
var options = new InstrumentationOptions(
5659
new[] { assemblyFileName },
5760
Array.Empty<string>(), OutputPath, PortNumber);
58-
Instrumenter instrumenter = new Instrumenter(options, new ConsoleLogger());
61+
Instrumenter instrumenter = new Instrumenter(options, _logger);
5962
instrumenter.BackupFolder = Path.Combine(OutputPath, "backup");
6063
Directory.CreateDirectory(instrumenter.BackupFolder);
6164
Map = instrumenter.Instrument();
@@ -74,7 +77,7 @@ public void RunSomeCode()
7477
{
7578
if (InstrumentedAssemblyPath == null)
7679
throw new InvalidOperationException("Sample assembly is not instrumented.");
77-
var client = RecordingController.ForEndPoint("localhost", PortNumber, Map);
80+
var client = RecordingController.ForEndPoint("localhost", PortNumber, Map, _logger);
7881
Assembly.LoadFrom(Path.Combine(OutputPath, "SG.CodeCoverage.Recorder.dll"));
7982
var assembly = Assembly.LoadFrom(InstrumentedAssemblyPath);
8083
var calc = assembly.DefinedTypes.Where(x => x.Name == nameof(PrimeCalculator)).FirstOrDefault();
@@ -85,7 +88,7 @@ public List<string> GetVisitedFiles()
8588
{
8689
if (InstrumentedAssemblyPath == null)
8790
throw new InvalidOperationException("Sample assembly is not instrumented.");
88-
var client = RecordingController.ForEndPoint("localhost", PortNumber, Map);
91+
var client = RecordingController.ForEndPoint("localhost", PortNumber, Map, _logger);
8992
return client.CollectResultAndReset().GetVisitedSources().ToList(); ;
9093
}
9194
}

SG.CodeCoverage/Collection/MultiRecordingController.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
using SG.CodeCoverage.Coverage;
1+
using SG.CodeCoverage.Common;
2+
using SG.CodeCoverage.Coverage;
23
using SG.CodeCoverage.Metadata;
34
using SG.CodeCoverage.Recorder.RecordingController;
45
using System;
56
using System.Collections.Generic;
67
using System.IO;
8+
using System.Linq;
79

810
namespace SG.CodeCoverage.Collection
911
{
1012
internal class MultiRecordingController : IRecordingController
1113
{
1214
private readonly string _recorderRuntimeConfigFilePath;
1315
private readonly InstrumentationMap _map;
16+
private readonly ILogger _logger;
1417
private DateTime _currentRuntimeConfigFileLastUpdate;
1518
private RuntimeConfig _currentRuntimeConfig;
1619
private const string _host = "localhost";
1720

18-
public MultiRecordingController(string recorderRuntimeConfigFilePath, InstrumentationMap map)
21+
public MultiRecordingController(string recorderRuntimeConfigFilePath, InstrumentationMap map, ILogger logger = null)
1922
{
2023
_recorderRuntimeConfigFilePath = recorderRuntimeConfigFilePath;
2124
_map = map;
25+
_logger = logger ?? new ConsoleLogger();
2226
}
2327

2428
public void ResetHits()
@@ -27,6 +31,7 @@ public void ResetHits()
2731
return;
2832
foreach(var process in _currentRuntimeConfig.Processes)
2933
{
34+
_logger.LogInformation($"Resetting hits for process {process.ID} on port {process.ListeningPort}");
3035
RecordingControllerClient.ResetHits(_host, process.ListeningPort);
3136
}
3237
}
@@ -38,6 +43,7 @@ public CoverageResult CollectResultAndReset()
3843
CoverageResult result = null;
3944
foreach(var process in _currentRuntimeConfig.Processes)
4045
{
46+
_logger.LogInformation($"Collecting coverage result from process {process.ID} on port {process.ListeningPort}");
4147
var procResult = RecordingControllerClient.CollectResultAndReset(_host, process.ListeningPort, _map);
4248
if (result == null)
4349
result = procResult;
@@ -53,15 +59,20 @@ private bool LoadRuntimeConfig()
5359
{
5460
_currentRuntimeConfigFileLastUpdate = default;
5561
_currentRuntimeConfig = null;
62+
_logger.LogWarning($"Runtime Config file '{_recorderRuntimeConfigFilePath}' not found.");
5663
return false;
5764
}
5865

5966
var newUpdateDate = File.GetLastWriteTime(_recorderRuntimeConfigFilePath);
6067
if (_currentRuntimeConfig == null ||
6168
newUpdateDate > _currentRuntimeConfigFileLastUpdate)
6269
{
70+
_logger.LogInformation($"Loading Runtime Config file '{_recorderRuntimeConfigFilePath}'");
6371
_currentRuntimeConfig = RuntimeConfig.Load(_recorderRuntimeConfigFilePath);
6472
_currentRuntimeConfigFileLastUpdate = newUpdateDate;
73+
_logger.LogVerbose($"Runtime Config file loaded. " +
74+
$"Processes: {string.Join(", ", _currentRuntimeConfig.Processes.Select(p => p.ID))}" +
75+
$" - Last update: {newUpdateDate:O}");
6576
}
6677
return true;
6778
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
using SG.CodeCoverage.Metadata;
1+
using SG.CodeCoverage.Common;
2+
using SG.CodeCoverage.Metadata;
23

34
namespace SG.CodeCoverage.Collection
45
{
56
public static class RecordingController
67
{
7-
public static IRecordingController ForRuntimeConfigFile(string runtimeConfigFilePath, InstrumentationMap map)
8+
public static IRecordingController ForRuntimeConfigFile(string runtimeConfigFilePath, InstrumentationMap map, ILogger logger)
89
{
9-
return new MultiRecordingController(runtimeConfigFilePath, map);
10+
return new MultiRecordingController(runtimeConfigFilePath, map, logger);
1011
}
1112

12-
public static IRecordingController ForEndPoint(string host, int port, InstrumentationMap map)
13+
public static IRecordingController ForEndPoint(string host, int port, InstrumentationMap map, ILogger logger)
1314
{
14-
return new SingleRecordingController(host, port, map);
15+
return new SingleRecordingController(host, port, map, logger);
1516
}
1617
}
1718
}

SG.CodeCoverage/Collection/SingleRecordingController.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using SG.CodeCoverage.Coverage;
1+
using SG.CodeCoverage.Common;
2+
using SG.CodeCoverage.Coverage;
23
using SG.CodeCoverage.Metadata;
34

45
namespace SG.CodeCoverage.Collection
@@ -8,21 +9,25 @@ internal class SingleRecordingController : IRecordingController
89
private readonly string _host;
910
private readonly int _port;
1011
private readonly InstrumentationMap _map;
12+
private readonly ILogger _logger;
1113

12-
public SingleRecordingController(string host, int port, InstrumentationMap map)
14+
public SingleRecordingController(string host, int port, InstrumentationMap map, ILogger logger = null)
1315
{
1416
_host = host;
1517
_port = port;
1618
_map = map;
19+
_logger = logger ?? new ConsoleLogger();
1720
}
1821

1922
public CoverageResult CollectResultAndReset()
2023
{
24+
_logger.LogInformation($"Collecting coverage result for {_host}:{_port}");
2125
return RecordingControllerClient.CollectResultAndReset(_host, _port, _map);
2226
}
2327

2428
public void ResetHits()
2529
{
30+
_logger.LogInformation($"Resetting hits for {_host}:{_port}");
2631
RecordingControllerClient.ResetHits(_host, _port);
2732
}
2833
}

0 commit comments

Comments
 (0)