Skip to content

Commit 49151d4

Browse files
authored
Use null-logger (Buildalyzer#268)
1 parent f23b366 commit 49151d4

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/Buildalyzer/Environment/EnvironmentFactory.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Buildalyzer.Construction;
44
using Microsoft.Build.Utilities;
55
using Microsoft.Extensions.Logging;
6+
using Microsoft.Extensions.Logging.Abstractions;
67
using NuGet.Frameworks;
78

89
namespace Buildalyzer.Environment;
@@ -11,13 +12,13 @@ public class EnvironmentFactory
1112
{
1213
private readonly IAnalyzerManager _manager;
1314
private readonly IProjectFile _projectFile;
14-
private readonly ILogger<EnvironmentFactory> _logger;
15+
private readonly ILogger Logger;
1516

1617
internal EnvironmentFactory(IAnalyzerManager manager, IProjectFile projectFile)
1718
{
1819
_manager = manager;
1920
_projectFile = projectFile;
20-
_logger = _manager.LoggerFactory?.CreateLogger<EnvironmentFactory>();
21+
Logger = _manager.LoggerFactory?.CreateLogger<EnvironmentFactory>() ?? NullLogger<EnvironmentFactory>.Instance;
2122
}
2223

2324
public BuildEnvironment? GetBuildEnvironment() =>
@@ -62,7 +63,7 @@ internal EnvironmentFactory(IAnalyzerManager manager, IProjectFile projectFile)
6263

6364
if ((info.BasePath ?? info.Runtimes.Values.FirstOrDefault()) is not { } dotnetPath)
6465
{
65-
_logger?.LogWarning("Could not locate SDK path in `{DotnetPath} --info` results", options.DotnetExePath);
66+
Logger.LogWarning("Could not locate SDK path in `{DotnetPath} --info` results", options.DotnetExePath);
6667
return null;
6768
}
6869

@@ -140,7 +141,7 @@ internal EnvironmentFactory(IAnalyzerManager manager, IProjectFile projectFile)
140141
}
141142
else if (!GetFrameworkMsBuildExePath(out msBuildExePath))
142143
{
143-
_logger?.LogWarning("Couldn't find a .NET Framework MSBuild path");
144+
Logger.LogWarning("Couldn't find a .NET Framework MSBuild path");
144145
return null;
145146
}
146147

src/Buildalyzer/Environment/ProcessRunner.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
using Microsoft.Extensions.Logging;
2+
using Microsoft.Extensions.Logging.Abstractions;
23

34
namespace Buildalyzer.Environment;
45

56
internal class ProcessRunner : IDisposable
67
{
7-
private readonly ILogger<ProcessRunner> _logger;
8+
private readonly ILogger Logger;
89

910
public List<string> Output { get; } = new List<string>();
1011
public List<string> Error { get; } = new List<string>();
12+
1113
public int ExitCode => Process.ExitCode;
1214

1315
private Process Process { get; }
@@ -21,7 +23,7 @@ public ProcessRunner(
2123
Dictionary<string, string?> environmentVariables,
2224
ILoggerFactory? loggerFactory)
2325
{
24-
_logger = loggerFactory?.CreateLogger<ProcessRunner>();
26+
Logger = loggerFactory?.CreateLogger<ProcessRunner>() ?? NullLogger<ProcessRunner>.Instance;
2527
Process = new Process
2628
{
2729
StartInfo =
@@ -54,15 +56,15 @@ public ProcessRunner(
5456
if (!string.IsNullOrEmpty(e.Data))
5557
{
5658
Output.Add(e.Data);
57-
_logger?.LogDebug(e.Data + System.Environment.NewLine);
59+
Logger.LogDebug("{Data}{NewLine}", e.Data, System.Environment.NewLine);
5860
}
5961
};
6062
Process.ErrorDataReceived += (_, e) =>
6163
{
6264
if (!string.IsNullOrEmpty(e.Data))
6365
{
6466
Error.Add(e.Data);
65-
_logger?.LogError(e.Data + System.Environment.NewLine);
67+
Logger.LogDebug("{Data}{NewLine}", e.Data, System.Environment.NewLine);
6668
}
6769
};
6870
}
@@ -72,14 +74,23 @@ public ProcessRunner Start()
7274
Process.Start();
7375
Process.BeginOutputReadLine();
7476
Process.BeginErrorReadLine();
75-
_logger?.LogDebug($"{System.Environment.NewLine}Started process {Process.Id}: \"{Process.StartInfo.FileName}\" {Process.StartInfo.Arguments}{System.Environment.NewLine}");
77+
Logger.LogDebug(
78+
"Started process {ProcessId}: \"{FileName}\" {Arguments}{NewLine}",
79+
Process.Id,
80+
Process.StartInfo.FileName,
81+
Process.StartInfo.Arguments,
82+
System.Environment.NewLine);
7683
return this;
7784
}
7885

79-
private void ProcessExited(object sender, EventArgs e)
86+
private void ProcessExited(object? sender, EventArgs e)
8087
{
8188
Exited?.Invoke();
82-
_logger?.LogDebug($"Process {Process.Id} exited with code {Process.ExitCode}{System.Environment.NewLine}{System.Environment.NewLine}");
89+
Logger.LogDebug(
90+
"Process {Id} exited with code {ExitCode}{NewLine}",
91+
Process.Id,
92+
Process.ExitCode,
93+
System.Environment.NewLine);
8394
}
8495

8596
public void WaitForExit() => Process.WaitForExit();

0 commit comments

Comments
 (0)