Skip to content

Use Azure DevOps logger formatting commands #1702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
26 changes: 16 additions & 10 deletions src/Microsoft.DotNet.ImageBuilder/src/ExecuteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ private static string Execute(
{
info.RedirectStandardError = true;
executeMessageOverride ??= $"{info.FileName} {info.Arguments}";
string prefix = isDryRun ? "EXECUTING [DRY RUN]" : "EXECUTING";
s_loggerService.WriteSubheading($"{prefix}: {executeMessageOverride}");
string prefix = isDryRun ? "Executing (dry-run)" : "Executing";
s_loggerService.WriteCommand($"{prefix}: {executeMessageOverride}");

if (isDryRun)
{
Expand All @@ -87,13 +87,16 @@ private static string Execute(
stopwatch.Start();
ProcessResult processResult = executor(info);
stopwatch.Stop();
s_loggerService.WriteSubheading($"EXECUTION ELAPSED TIME: {stopwatch.Elapsed}");
s_loggerService.WriteMessage($"Execution elapsed time: {stopwatch.Elapsed}");

if (processResult.Process.ExitCode != 0)
{
string exceptionMsg = errorMessage ?? $@"Failed to execute {info.FileName} {info.Arguments}
string exceptionMsg = errorMessage ??
$"""
Failed to execute {executeMessageOverride}

{processResult.StandardError}";
{processResult.StandardError}
""";

throw new InvalidOperationException(exceptionMsg);
}
Expand Down Expand Up @@ -131,11 +134,14 @@ DataReceivedEventHandler getDataReceivedHandler(StringBuilder stringBuilder, Tex
StringBuilder stdError = new StringBuilder();
process.ErrorDataReceived += getDataReceivedHandler(stdError, Console.Error);

process.Start();
processStartedCallback?.Invoke(process);
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
using (s_loggerService.LogGroup("Command output"))
{
process.Start();
processStartedCallback?.Invoke(process);
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}

return new ProcessResult(process, stdOutput.ToString().Trim(), stdError.ToString().Trim());
}
Expand Down
72 changes: 63 additions & 9 deletions src/Microsoft.DotNet.ImageBuilder/src/ILoggerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,68 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace Microsoft.DotNet.ImageBuilder
#nullable enable

using System;

namespace Microsoft.DotNet.ImageBuilder;

public interface ILoggerService
{
public interface ILoggerService
{
void WriteError(string error);
void WriteHeading(string heading);
void WriteMessage();
void WriteMessage(string message);
void WriteSubheading(string subheading);
}
/// <summary>
/// Writes an error message to the log.
/// </summary>
/// <param name="error">The error message to log.</param>
void WriteError(string error);

/// <summary>
/// Writes a heading to the log, typically used for major sections or
/// operations.
/// </summary>
/// <param name="heading">The heading text to log.</param>
void WriteHeading(string heading);

/// <summary>
/// Writes a general message to the log.
/// </summary>
/// <param name="message">The message to log. Can be null.</param>
void WriteMessage(string? message = null);

/// <summary>
/// Writes a subheading to the log, typically used for sub-sections within
/// a headed section.
/// </summary>
/// <param name="subheading">The subheading text to log.</param>
void WriteSubheading(string subheading);

/// <summary>
/// Writes a warning message to the log.
/// </summary>
/// <param name="message">The warning message to log.</param>
void WriteWarning(string message);

/// <summary>
/// Writes a debug message to the log that might only be shown in verbose
/// logging modes.
/// </summary>
/// <param name="message">The debug message to log.</param>
void WriteDebug(string message);

/// <summary>
/// Writes a command execution message to the log. This does not actually
/// execute the command or capture its output. It is intended to record
/// that a command was executed.
/// </summary>
/// <param name="command">
/// The command to log, including executable name and all arguments
/// </param>
void WriteCommand(string command);

/// <summary>
/// Creates a logical grouping of log messages. Useful for grouping large
/// amounts of related text, like executable/process output.
/// </summary>
/// <param name="name">The name of the group.</param>
/// <returns>An IDisposable that when disposed will end the group.</returns>
IDisposable LogGroup(string name);
}
67 changes: 39 additions & 28 deletions src/Microsoft.DotNet.ImageBuilder/src/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,48 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using System;

namespace Microsoft.DotNet.ImageBuilder
namespace Microsoft.DotNet.ImageBuilder;

public static class Logger
{
public static class Logger
public static void WriteError(string error)
{
Console.Error.WriteLine($"##[error]{error}");
}

public static void WriteHeading(string heading)
{
Console.WriteLine();
Console.WriteLine(heading);
Console.WriteLine(new string('-', heading.Length));
}

public static void WriteMessage(string? message = null)
{
Console.WriteLine(message);
}

public static void WriteSubheading(string subheading)
{
WriteMessage($"##[section]{subheading}");
}

public static void WriteCommand(string command)
{
WriteMessage($"##[command]{command}");
}

public static void WriteWarning(string message)
{
WriteMessage($"##[warning]{message}");
}

public static void WriteDebug(string message)
{
public static void WriteError(string error)
{
Console.Error.WriteLine(error);
}

public static void WriteHeading(string heading)
{
Console.WriteLine();
Console.WriteLine(heading);
Console.WriteLine(new string('-', heading.Length));
}

public static void WriteMessage()
{
Console.WriteLine();
}

public static void WriteMessage(string message)
{
Console.WriteLine(message);
}

public static void WriteSubheading(string subheading)
{
WriteMessage($"-- {subheading}");
}
WriteMessage($"##[debug]{message}");
}
}
38 changes: 32 additions & 6 deletions src/Microsoft.DotNet.ImageBuilder/src/LoggerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,62 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using System;
using System.ComponentModel.Composition;

namespace Microsoft.DotNet.ImageBuilder
{
[Export(typeof(ILoggerService))]
internal class LoggerService : ILoggerService
{
/// <inheritdoc />
public void WriteError(string error)
{
Logger.WriteError(error);
}

/// <inheritdoc />
public void WriteHeading(string heading)
{
Logger.WriteHeading(heading);
}

public void WriteMessage()
{
Logger.WriteMessage();
}

public void WriteMessage(string message)
/// <inheritdoc />
public void WriteMessage(string? message = null)
{
Logger.WriteMessage(message);
}

/// <inheritdoc />
public void WriteSubheading(string subheading)
{
Logger.WriteSubheading(subheading);
}

/// <inheritdoc />
public void WriteCommand(string command)
{
Logger.WriteCommand(command);
}

/// <inheritdoc />
public void WriteWarning(string message)
{
Logger.WriteWarning(message);
}

/// <inheritdoc />
public void WriteDebug(string message)
{
Logger.WriteDebug(message);
}

/// <inheiritdoc />
public IDisposable LogGroup(string name)
{
return new LoggingGroup(name, this);
}
}
}
44 changes: 44 additions & 0 deletions src/Microsoft.DotNet.ImageBuilder/src/LoggingGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using System;

namespace Microsoft.DotNet.ImageBuilder;

/// <summary>
/// Manages an Azure Pipelines collapsible logging group.
/// Disposing of the object closes the logging group.
/// See https://learn.microsoft.com/azure/devops/pipelines/scripts/logging-commands
/// </summary>
/// <remarks>
/// Only one LoggingGroup can be open at a time. If a second group is created
/// without closing the first, then the first group will not be collapsible.
Comment on lines +17 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be tough to notice while writing code. Can we enforce this from the logging service?

/// </remarks>
internal sealed class LoggingGroup : IDisposable
{
private readonly ILoggerService _logger;

/// <summary>
/// Creates a new collapsible logging group. When this object is created,
/// all subsequent logging output will be inside this group until this
/// object is disposed.
/// </summary>
/// <param name="groupName">The name of the logging group</param>
/// <param name="loggerService">The logger service to use for output</param>
public LoggingGroup(string name, ILoggerService loggerService)
{
_logger = loggerService;
_logger.WriteMessage($"##[group]{name}");
}

/// <summary>
/// Ends the current collapsible logging group.
/// </summary>
public void Dispose()
{
_logger.WriteMessage($"##[endgroup]");
}
}
Loading