Skip to content

Commit aec1f02

Browse files
authored
Add more verbose logging during the build (#208)
pervasive logging Better initial log
1 parent 7f7a2e1 commit aec1f02

File tree

5 files changed

+24
-26
lines changed

5 files changed

+24
-26
lines changed

Microsoft.NET.Build.Containers/ContainerBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static async Task Containerize(DirectoryInfo folder, string workingDir, s
7272
{
7373
try
7474
{
75-
outputReg?.Push(img, imageName, tag, imageName).Wait();
75+
outputReg?.Push(img, imageName, tag, imageName, (message) => Console.WriteLine($"Containerize: {message}")).Wait();
7676
Console.WriteLine($"Containerize: Pushed container '{imageName}:{tag}' to registry '{outputRegistry}'");
7777
}
7878
catch (Exception e)

Microsoft.NET.Build.Containers/CreateNewImage.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ private Image GetBaseImage() {
193193
}
194194
}
195195

196+
private void SafeLog(string message, params object[] formatParams) {
197+
if(BuildEngine != null) Log.LogMessage(MessageImportance.High, message, formatParams);
198+
}
199+
196200
public override bool Execute()
197201
{
198202
if (!Directory.Exists(PublishDirectory))
@@ -203,10 +207,7 @@ public override bool Execute()
203207

204208
var image = GetBaseImage();
205209

206-
if (BuildEngine != null)
207-
{
208-
Log.LogMessage($"Loading from directory: {PublishDirectory}");
209-
}
210+
SafeLog("Building image '{0}' with tags {1} on top of base image {2}/{3}:{4}", ImageName, String.Join(",", ImageTags), BaseRegistry, BaseImageName, BaseImageTag);
210211

211212
Layer newLayer = Layer.FromDirectory(PublishDirectory, WorkingDirectory);
212213
image.AddLayer(newLayer);
@@ -240,10 +241,7 @@ public override bool Execute()
240241
try
241242
{
242243
LocalDocker.Load(image, ImageName, tag, BaseImageName).Wait();
243-
if (BuildEngine != null)
244-
{
245-
Log.LogMessage(MessageImportance.High, "Pushed container '{0}:{1}' to Docker daemon", ImageName, tag);
246-
}
244+
SafeLog("Pushed container '{0}:{1}' to Docker daemon", ImageName, tag);
247245
}
248246
catch (AggregateException ex) when (ex.InnerException is DockerLoadException dle)
249247
{
@@ -254,11 +252,8 @@ public override bool Execute()
254252
{
255253
try
256254
{
257-
outputReg?.Push(image, ImageName, tag, BaseImageName).Wait();
258-
if (BuildEngine != null)
259-
{
260-
Log.LogMessage(MessageImportance.High, "Pushed container '{0}:{1}' to registry '{2}'", ImageName, tag, OutputRegistry);
261-
}
255+
outputReg?.Push(image, ImageName, tag, BaseImageName, message => SafeLog(message)).Wait();
256+
SafeLog("Pushed container '{0}:{1}' to registry '{2}'", ImageName, tag, OutputRegistry);
262257
}
263258
catch (Exception e)
264259
{

Microsoft.NET.Build.Containers/ParseContainerProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public override bool Execute()
165165
{
166166
if (!ContainerHelpers.NormalizeImageName(ContainerImageName, out string? normalizedImageName))
167167
{
168-
Log.LogMessage(MessageImportance.High, $"{nameof(ContainerImageName)}:'{ContainerImageName}' was not a valid container image name, it was normalized to {normalizedImageName}");
168+
Log.LogMessage(MessageImportance.High, $"'{ContainerImageName}' was not a valid container image name, it was normalized to {normalizedImageName}");
169169
NewContainerImageName = normalizedImageName ?? "";
170170
}
171171
else

Microsoft.NET.Build.Containers/Registry.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public async Task<string> DownloadBlob(string name, Descriptor descriptor)
8989
return localPath;
9090
}
9191

92-
public async Task Push(Layer layer, string name)
92+
public async Task Push(Layer layer, string name, Action<string> logProgressMessage)
9393
{
9494
string digest = layer.Descriptor.Digest;
9595

@@ -169,7 +169,7 @@ private static HttpClient GetClient()
169169
return client;
170170
}
171171

172-
public async Task Push(Image x, string name, string? tag, string baseName)
172+
public async Task Push(Image x, string name, string? tag, string baseName, Action<string> logProgressMessage)
173173
{
174174
tag ??= "latest";
175175

@@ -178,9 +178,10 @@ public async Task Push(Image x, string name, string? tag, string baseName)
178178
foreach (var descriptor in x.LayerDescriptors)
179179
{
180180
string digest = descriptor.Digest;
181-
181+
logProgressMessage($"Uploading layer {digest} to registry");
182182
if (await BlobAlreadyUploaded(name, digest, client))
183183
{
184+
logProgressMessage($"Layer {digest} already existed");
184185
continue;
185186
}
186187

@@ -194,32 +195,34 @@ public async Task Push(Image x, string name, string? tag, string baseName)
194195
if (!x.originatingRegistry.HasValue)
195196
{
196197
throw new NotImplementedException("Need a good error for 'couldn't download a thing because no link to registry'");
197-
}
198+
}
198199

199200
// Ensure the blob is available locally
200201
await x.originatingRegistry.Value.DownloadBlob(x.OriginatingName, descriptor);
201-
202+
logProgressMessage($"Finished uploading layer {digest} to registry");
202203
// Then push it to the destination registry
203-
await Push(Layer.FromDescriptor(descriptor), name);
204+
await Push(Layer.FromDescriptor(descriptor), name, logProgressMessage);
204205
}
205206
}
206207

208+
logProgressMessage($"Uploading config to registry");
207209
using (MemoryStream stringStream = new MemoryStream(Encoding.UTF8.GetBytes(x.config.ToJsonString())))
208210
{
209211
await UploadBlob(name, x.GetDigest(x.config), stringStream);
212+
logProgressMessage($"Uploaded config to registry");
210213
}
211214

215+
logProgressMessage($"Uploading manifest to registry");
212216
HttpContent manifestUploadContent = new StringContent(x.manifest.ToJsonString());
213217
manifestUploadContent.Headers.ContentType = new MediaTypeHeaderValue(DockerManifestV2);
214-
215218
var putResponse = await client.PutAsync(new Uri(BaseUri, $"/v2/{name}/manifests/{x.GetDigest(x.manifest)}"), manifestUploadContent);
216-
217219
string putresponsestr = await putResponse.Content.ReadAsStringAsync();
218-
219220
putResponse.EnsureSuccessStatusCode();
221+
logProgressMessage($"Uploaded manifest to registry");
220222

223+
logProgressMessage($"Uploading tag to registry");
221224
var putResponse2 = await client.PutAsync(new Uri(BaseUri, $"/v2/{name}/manifests/{tag}"), manifestUploadContent);
222-
223225
putResponse2.EnsureSuccessStatusCode();
226+
logProgressMessage($"Uploaded tag to registry");
224227
}
225228
}

Test.Microsoft.NET.Build.Containers.Filesystem/EndToEnd.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public async Task ApiEndToEndWithRegistryPushAndPull()
4141

4242
// Push the image back to the local registry
4343

44-
await registry.Push(x, NewImageName(), "latest", DockerRegistryManager.BaseImage);
44+
await registry.Push(x, NewImageName(), "latest", DockerRegistryManager.BaseImage, Console.WriteLine);
4545

4646
// pull it back locally
4747

0 commit comments

Comments
 (0)