diff --git a/src/Testcontainers/Clients/DockerApiClient.cs b/src/Testcontainers/Clients/DockerApiClient.cs index a1c302c4d..dc07d52f7 100644 --- a/src/Testcontainers/Clients/DockerApiClient.cs +++ b/src/Testcontainers/Clients/DockerApiClient.cs @@ -8,6 +8,7 @@ namespace DotNet.Testcontainers.Clients using System.Threading; using System.Threading.Tasks; using Docker.DotNet; + using Docker.DotNet.Models; using DotNet.Testcontainers.Configurations; using JetBrains.Annotations; using Microsoft.Extensions.Logging; @@ -74,14 +75,60 @@ await RuntimeInitialized.WaitAsync(ct) if (!ProcessedHashCodes.Contains(hashCode)) { - await Logger.DockerRuntimeInfoAsync(DockerClient) + await LogDockerRuntimeInformationAsync(ct) .ConfigureAwait(false); + ProcessedHashCodes.Add(hashCode); } RuntimeInitialized.Release(); } + private async Task LogDockerRuntimeInformationAsync(CancellationToken ct) + { + SystemInfoResponse dockerInfo; + VersionResponse dockerVersion; + try + { + dockerInfo = await DockerClient.System.GetSystemInfoAsync(ct) + .ConfigureAwait(false); + + dockerVersion = await DockerClient.System.GetVersionAsync(ct) + .ConfigureAwait(false); + } + catch (Exception e) + { + Logger.LogError(e, "Failed to retrieve Docker container runtime information"); + return; + } + + var runtimeInfo = new StringBuilder(); + + var byteUnits = new[] { "KB", "MB", "GB" }; + + runtimeInfo.AppendLine("Connected to Docker:"); + + runtimeInfo.Append(" Host: "); + runtimeInfo.AppendLine(DockerClient.Configuration.EndpointBaseUri.ToString()); + + runtimeInfo.Append(" Server Version: "); + runtimeInfo.AppendLine(dockerInfo.ServerVersion); + + runtimeInfo.Append(" Kernel Version: "); + runtimeInfo.AppendLine(dockerInfo.KernelVersion); + + runtimeInfo.Append(" API Version: "); + runtimeInfo.AppendLine(dockerVersion.APIVersion); + + runtimeInfo.Append(" Operating System: "); + runtimeInfo.AppendLine(dockerInfo.OperatingSystem); + + runtimeInfo.Append(" Total Memory: "); + runtimeInfo.AppendFormat(CultureInfo.InvariantCulture, "{0:F} {1}", dockerInfo.MemTotal / Math.Pow(1024, byteUnits.Length), byteUnits[byteUnits.Length - 1]); + + Logger.LogInformation(runtimeInfo.ToString()); + } + private static IDockerClient GetDockerClient(Guid sessionId, IDockerEndpointAuthenticationConfiguration dockerEndpointAuthConfig) { using (var dockerClientConfiguration = dockerEndpointAuthConfig.GetDockerClientConfiguration(sessionId)) diff --git a/src/Testcontainers/Logging.cs b/src/Testcontainers/Logging.cs index f5b924c8b..8e1d5b07a 100644 --- a/src/Testcontainers/Logging.cs +++ b/src/Testcontainers/Logging.cs @@ -4,8 +4,6 @@ namespace DotNet.Testcontainers using System.Collections.Generic; using System.Text.Json; using System.Text.RegularExpressions; - using System.Threading.Tasks; - using Docker.DotNet; using DotNet.Testcontainers.Images; using Microsoft.Extensions.Logging; @@ -13,19 +11,6 @@ internal static class Logging { #pragma warning disable InconsistentNaming, SA1309 - private static readonly Action _DockerRuntimeInfo - = LoggerMessage.Define(LogLevel.Information, default, - "Connected to Docker" + Environment.NewLine + - " Host: {Host}" + Environment.NewLine + - " Server Version: {ServerVersion}" + Environment.NewLine + - " Kernel Version: {KernelVersion}" + Environment.NewLine + - " API Version: {APIVersion}" + Environment.NewLine + - " Operating System: {OperatingSystem}" + Environment.NewLine + - " Total Memory: {TotalMemory:F2} GB"); - - private static readonly Action _DockerRuntimeError - = LoggerMessage.Define(LogLevel.Error, default, "Failed to retrieve Docker container runtime information"); - private static readonly Action _IgnorePatternAdded = LoggerMessage.Define(LogLevel.Information, default, "Pattern {IgnorePattern} added to the regex cache"); @@ -127,23 +112,6 @@ private static readonly Action _ReusableResourceNotFound #pragma warning restore InconsistentNaming, SA1309 - public static async Task DockerRuntimeInfoAsync(this ILogger logger, IDockerClient dockerClient) - { - try - { - var dockerInfo = await dockerClient.System.GetSystemInfoAsync() - .ConfigureAwait(false); - var dockerVersion = await dockerClient.System.GetVersionAsync() - .ConfigureAwait(false); - var totalMemory = dockerInfo.MemTotal / (1024.0 * 1024 * 1024); - _DockerRuntimeInfo(logger, dockerClient.Configuration.EndpointBaseUri, dockerInfo.ServerVersion, dockerInfo.KernelVersion, dockerVersion.APIVersion, dockerInfo.OperatingSystem, totalMemory, null); - } - catch (Exception exception) - { - _DockerRuntimeError(logger, exception); - } - } - public static void IgnorePatternAdded(this ILogger logger, Regex ignorePattern) { _IgnorePatternAdded(logger, ignorePattern, null);