Skip to content

Commit

Permalink
Use null-logger (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel authored May 7, 2024
1 parent f23b366 commit 49151d4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
9 changes: 5 additions & 4 deletions src/Buildalyzer/Environment/EnvironmentFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Buildalyzer.Construction;
using Microsoft.Build.Utilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using NuGet.Frameworks;

namespace Buildalyzer.Environment;
Expand All @@ -11,13 +12,13 @@ public class EnvironmentFactory
{
private readonly IAnalyzerManager _manager;
private readonly IProjectFile _projectFile;
private readonly ILogger<EnvironmentFactory> _logger;
private readonly ILogger Logger;

internal EnvironmentFactory(IAnalyzerManager manager, IProjectFile projectFile)
{
_manager = manager;
_projectFile = projectFile;
_logger = _manager.LoggerFactory?.CreateLogger<EnvironmentFactory>();
Logger = _manager.LoggerFactory?.CreateLogger<EnvironmentFactory>() ?? NullLogger<EnvironmentFactory>.Instance;
}

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

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

Expand Down Expand Up @@ -140,7 +141,7 @@ internal EnvironmentFactory(IAnalyzerManager manager, IProjectFile projectFile)
}
else if (!GetFrameworkMsBuildExePath(out msBuildExePath))
{
_logger?.LogWarning("Couldn't find a .NET Framework MSBuild path");
Logger.LogWarning("Couldn't find a .NET Framework MSBuild path");
return null;
}

Expand Down
25 changes: 18 additions & 7 deletions src/Buildalyzer/Environment/ProcessRunner.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace Buildalyzer.Environment;

internal class ProcessRunner : IDisposable
{
private readonly ILogger<ProcessRunner> _logger;
private readonly ILogger Logger;

public List<string> Output { get; } = new List<string>();
public List<string> Error { get; } = new List<string>();

public int ExitCode => Process.ExitCode;

private Process Process { get; }
Expand All @@ -21,7 +23,7 @@ public ProcessRunner(
Dictionary<string, string?> environmentVariables,
ILoggerFactory? loggerFactory)
{
_logger = loggerFactory?.CreateLogger<ProcessRunner>();
Logger = loggerFactory?.CreateLogger<ProcessRunner>() ?? NullLogger<ProcessRunner>.Instance;
Process = new Process
{
StartInfo =
Expand Down Expand Up @@ -54,15 +56,15 @@ public ProcessRunner(
if (!string.IsNullOrEmpty(e.Data))
{
Output.Add(e.Data);
_logger?.LogDebug(e.Data + System.Environment.NewLine);
Logger.LogDebug("{Data}{NewLine}", e.Data, System.Environment.NewLine);
}
};
Process.ErrorDataReceived += (_, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
Error.Add(e.Data);
_logger?.LogError(e.Data + System.Environment.NewLine);
Logger.LogDebug("{Data}{NewLine}", e.Data, System.Environment.NewLine);
}
};
}
Expand All @@ -72,14 +74,23 @@ public ProcessRunner Start()
Process.Start();
Process.BeginOutputReadLine();
Process.BeginErrorReadLine();
_logger?.LogDebug($"{System.Environment.NewLine}Started process {Process.Id}: \"{Process.StartInfo.FileName}\" {Process.StartInfo.Arguments}{System.Environment.NewLine}");
Logger.LogDebug(
"Started process {ProcessId}: \"{FileName}\" {Arguments}{NewLine}",
Process.Id,
Process.StartInfo.FileName,
Process.StartInfo.Arguments,
System.Environment.NewLine);
return this;
}

private void ProcessExited(object sender, EventArgs e)
private void ProcessExited(object? sender, EventArgs e)
{
Exited?.Invoke();
_logger?.LogDebug($"Process {Process.Id} exited with code {Process.ExitCode}{System.Environment.NewLine}{System.Environment.NewLine}");
Logger.LogDebug(
"Process {Id} exited with code {ExitCode}{NewLine}",
Process.Id,
Process.ExitCode,
System.Environment.NewLine);
}

public void WaitForExit() => Process.WaitForExit();
Expand Down

0 comments on commit 49151d4

Please sign in to comment.