diff --git a/src/Cake.Common/Diagnostics/LoggingAliases.Disposable.cs b/src/Cake.Common/Diagnostics/LoggingAliases.Disposable.cs new file mode 100644 index 0000000000..8cf500e9c0 --- /dev/null +++ b/src/Cake.Common/Diagnostics/LoggingAliases.Disposable.cs @@ -0,0 +1,164 @@ +// 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. + +using System; +using Cake.Core; +using Cake.Core.Annotations; +using Cake.Core.Diagnostics; + +namespace Cake.Common.Diagnostics; + +public static partial class LoggingAliases +{ + /// + /// Sets the log verbosity to quiet and returns a disposable that restores the log verbosity on dispose. + /// + /// the context. + /// A disposable that restores the log verbosity. + /// + /// + /// using (QuietVerbosity()) + /// { + /// Error("Show me."); + /// Warning("Hide me."); + /// Information("Hide me."); + /// Verbose("Hide me."); + /// Debug("Hide me."); + /// } + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Verbosity")] + public static IDisposable QuietVerbosity(this ICakeContext context) + { + ArgumentNullException.ThrowIfNull(context); + return context.Log.QuietVerbosity(); + } + + /// + /// Sets the log verbosity to minimal and returns a disposable that restores the log verbosity on dispose. + /// + /// the context. + /// A disposable that restores the log verbosity. + /// + /// + /// using (MinimalVerbosity()) + /// { + /// Error("Show me."); + /// Warning("Show me."); + /// Information("Hide me."); + /// Verbose("Hide me."); + /// Debug("Hide me."); + /// } + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Verbosity")] + public static IDisposable MinimalVerbosity(this ICakeContext context) + { + ArgumentNullException.ThrowIfNull(context); + return context.Log.MinimalVerbosity(); + } + + /// + /// Sets the log verbosity to normal and returns a disposable that restores the log verbosity on dispose. + /// + /// the context. + /// A disposable that restores the log verbosity. + /// + /// + /// using (NormalVerbosity()) + /// { + /// Error("Show me."); + /// Warning("Show me."); + /// Information("Show me."); + /// Verbose("Hide me."); + /// Debug("Hide me."); + /// } + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Verbosity")] + public static IDisposable NormalVerbosity(this ICakeContext context) + { + ArgumentNullException.ThrowIfNull(context); + return context.Log.NormalVerbosity(); + } + + /// + /// Sets the log verbosity to verbose and returns a disposable that restores the log verbosity on dispose. + /// + /// the context. + /// A disposable that restores the log verbosity. + /// + /// + /// using (VerboseVerbosity()) + /// { + /// Error("Show me."); + /// Warning("Show me."); + /// Information("Show me."); + /// Verbose("Show me."); + /// Debug("Hide me."); + /// } + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Verbosity")] + public static IDisposable VerboseVerbosity(this ICakeContext context) + { + ArgumentNullException.ThrowIfNull(context); + return context.Log.VerboseVerbosity(); + } + + /// + /// Sets the log verbosity to diagnostic and returns a disposable that restores the log verbosity on dispose. + /// + /// the context. + /// A disposable that restores the log verbosity. + /// + /// + /// using (DiagnosticVerbosity()) + /// { + /// Error("Show me."); + /// Warning("Show me."); + /// Information("Show me."); + /// Verbose("Show me."); + /// Debug("Show me."); + /// } + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Verbosity")] + public static IDisposable DiagnosticVerbosity(this ICakeContext context) + { + ArgumentNullException.ThrowIfNull(context); + return context.Log.DiagnosticVerbosity(); + } + + /// + /// Sets the log verbosity as specified and returns a disposable that restores the log verbosity on dispose. + /// + /// The context. + /// The verbosity. + /// A disposable that restores the log verbosity. + /// + /// + /// using (DiagnosticVerbosity()) + /// { + /// Error("Show me."); + /// Warning("Show me."); + /// Information("Show me."); + /// Verbose("Show me."); + /// Debug("Show me."); + /// } + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Verbosity")] + public static IDisposable WithVerbosity(this ICakeContext context, Verbosity verbosity) + { + ArgumentNullException.ThrowIfNull(context); + return context.Log.WithVerbosity(verbosity); + } +} diff --git a/src/Cake.Common/Diagnostics/LoggingAliases.Formattable.cs b/src/Cake.Common/Diagnostics/LoggingAliases.Formattable.cs new file mode 100644 index 0000000000..664f13da47 --- /dev/null +++ b/src/Cake.Common/Diagnostics/LoggingAliases.Formattable.cs @@ -0,0 +1,189 @@ +using System; +using Cake.Core; +using Cake.Core.Annotations; +using Cake.Core.Diagnostics; + +namespace Cake.Common.Diagnostics; + +public static partial class LoggingAliases +{ + /// + /// Writes an error message to the log using the specified format information. + /// + /// The context. + /// The string to be formatted. + /// + /// + /// Error($"Hello {"World"}! Today is an {DateTime.Now:dddd}"); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Error")] + public static void Error(this ICakeContext context, FormattableString formattable) + { + ArgumentNullException.ThrowIfNull(context); + context.Log.Error(formattable); + } + + /// + /// Writes an warning message to the log using the specified format information. + /// + /// The context. + /// The string to be formatted. + /// + /// + /// Warning($"Hello {"World"}! Today is an {DateTime.Now:dddd}"); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Warning")] + public static void Warning(this ICakeContext context, FormattableString formattable) + { + ArgumentNullException.ThrowIfNull(context); + context.Log.Warning(formattable); + } + + /// + /// Writes an informational message to the log using the specified formattable string. + /// + /// The context. + /// The string to be formatted. + /// + /// + /// Information($"Hello {"World"}! Today is an {DateTime.Now:dddd}"); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Information")] + public static void Information(this ICakeContext context, FormattableString formattable) + { + ArgumentNullException.ThrowIfNull(context); + context.Log.Information(formattable); + } + + /// + /// Writes an verbose message to the log using the specified formattable string. + /// + /// The context. + /// The string to be formatted. + /// + /// + /// Verbose($"Hello {"World"}! Today is an {DateTime.Now:dddd}"); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Verbose")] + public static void Verbose(this ICakeContext context, FormattableString formattable) + { + ArgumentNullException.ThrowIfNull(context); + context.Log.Verbose(formattable); + } + + /// + /// Writes an debug message to the log using the specified formattable string. + /// + /// The context. + /// The string to be formatted. + /// + /// + /// Debug($"Hello {"World"}! Today is an {DateTime.Now:dddd}"); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Debug")] + public static void Debug(this ICakeContext context, FormattableString formattable) + { + ArgumentNullException.ThrowIfNull(context); + context.Log.Debug(formattable); + } + + /// + /// Writes an error message to the log using the specified format information. + /// + /// The context. + /// The log action. + /// + /// + /// Error(logAction => logAction($"Hello {"World"}! Today is an {DateTime.Now:dddd}")); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Error")] + public static void Error(this ICakeContext context, FormattableLogAction formattableLogAction) + { + ArgumentNullException.ThrowIfNull(context); + context.Log.Error(formattableLogAction); + } + + /// + /// Writes an warning message to the log using the specified format information. + /// + /// The context. + /// The log action. + /// + /// + /// Warning(logAction => logAction($"Hello {"World"}! Today is an {DateTime.Now:dddd}")); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Warning")] + public static void Warning(this ICakeContext context, FormattableLogAction formattableLogAction) + { + ArgumentNullException.ThrowIfNull(context); + context.Log.Warning(formattableLogAction); + } + + /// + /// Writes an informational message to the log using the specified formattable string. + /// + /// The context. + /// The log action. + /// + /// + /// Information(logAction => logAction($"Hello {"World"}! Today is an {DateTime.Now:dddd}")); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Information")] + public static void Information(this ICakeContext context, FormattableLogAction formattableLogAction) + { + ArgumentNullException.ThrowIfNull(context); + context.Log.Information(formattableLogAction); + } + + /// + /// Writes an verbose message to the log using the specified formattable string. + /// + /// The context. + /// The log action. + /// + /// + /// Verbose(logAction => logAction($"Hello {"World"}! Today is an {DateTime.Now:dddd}")); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Verbose")] + public static void Verbose(this ICakeContext context, FormattableLogAction formattableLogAction) + { + ArgumentNullException.ThrowIfNull(context); + context.Log.Verbose(formattableLogAction); + } + + /// + /// Writes an debug message to the log using the specified formattable string. + /// + /// The context. + /// The log action. + /// + /// + /// Debug(logAction => logAction($"Hello {"World"}! Today is an {DateTime.Now:dddd}")); + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Debug")] + public static void Debug(this ICakeContext context, FormattableLogAction formattableLogAction) + { + ArgumentNullException.ThrowIfNull(context); + context.Log.Debug(formattableLogAction); + } +} diff --git a/src/Cake.Common/Diagnostics/LoggingAliases.cs b/src/Cake.Common/Diagnostics/LoggingAliases.cs index efa33f839a..3e7d5f8ea3 100644 --- a/src/Cake.Common/Diagnostics/LoggingAliases.cs +++ b/src/Cake.Common/Diagnostics/LoggingAliases.cs @@ -13,7 +13,7 @@ namespace Cake.Common.Diagnostics /// Contains functionality related to logging. /// [CakeAliasCategory("Logging")] - public static class LoggingAliases + public static partial class LoggingAliases { /// /// Writes an error message to the log using the specified format information. @@ -384,130 +384,5 @@ public static void Debug(this ICakeContext context, string value) ArgumentNullException.ThrowIfNull(context); context.Log.Debug(value); } - - /// - /// Sets the log verbosity to quiet and returns a disposable that restores the log verbosity on dispose. - /// - /// the context. - /// A disposable that restores the log verbosity. - /// - /// - /// using (QuietVerbosity()) - /// { - /// Error("Show me."); - /// Warning("Hide me."); - /// Information("Hide me."); - /// Verbose("Hide me."); - /// Debug("Hide me."); - /// } - /// - /// - [CakeMethodAlias] - [CakeAliasCategory("Verbosity")] - public static IDisposable QuietVerbosity(this ICakeContext context) - { - ArgumentNullException.ThrowIfNull(context); - return context.Log.QuietVerbosity(); - } - - /// - /// Sets the log verbosity to minimal and returns a disposable that restores the log verbosity on dispose. - /// - /// the context. - /// A disposable that restores the log verbosity. - /// - /// - /// using (MinimalVerbosity()) - /// { - /// Error("Show me."); - /// Warning("Show me."); - /// Information("Hide me."); - /// Verbose("Hide me."); - /// Debug("Hide me."); - /// } - /// - /// - [CakeMethodAlias] - [CakeAliasCategory("Verbosity")] - public static IDisposable MinimalVerbosity(this ICakeContext context) - { - ArgumentNullException.ThrowIfNull(context); - return context.Log.MinimalVerbosity(); - } - - /// - /// Sets the log verbosity to normal and returns a disposable that restores the log verbosity on dispose. - /// - /// the context. - /// A disposable that restores the log verbosity. - /// - /// - /// using (NormalVerbosity()) - /// { - /// Error("Show me."); - /// Warning("Show me."); - /// Information("Show me."); - /// Verbose("Hide me."); - /// Debug("Hide me."); - /// } - /// - /// - [CakeMethodAlias] - [CakeAliasCategory("Verbosity")] - public static IDisposable NormalVerbosity(this ICakeContext context) - { - ArgumentNullException.ThrowIfNull(context); - return context.Log.NormalVerbosity(); - } - - /// - /// Sets the log verbosity to verbose and returns a disposable that restores the log verbosity on dispose. - /// - /// the context. - /// A disposable that restores the log verbosity. - /// - /// - /// using (VerboseVerbosity()) - /// { - /// Error("Show me."); - /// Warning("Show me."); - /// Information("Show me."); - /// Verbose("Show me."); - /// Debug("Hide me."); - /// } - /// - /// - [CakeMethodAlias] - [CakeAliasCategory("Verbosity")] - public static IDisposable VerboseVerbosity(this ICakeContext context) - { - ArgumentNullException.ThrowIfNull(context); - return context.Log.VerboseVerbosity(); - } - - /// - /// Sets the log verbosity to diagnostic and returns a disposable that restores the log verbosity on dispose. - /// - /// the context. - /// A disposable that restores the log verbosity. - /// - /// - /// using (DiagnosticVerbosity()) - /// { - /// Error("Show me."); - /// Warning("Show me."); - /// Information("Show me."); - /// Verbose("Show me."); - /// Debug("Show me."); - /// } - /// - /// - [CakeMethodAlias] - [CakeAliasCategory("Verbosity")] - public static IDisposable DiagnosticVerbosity(this ICakeContext context) - { - ArgumentNullException.ThrowIfNull(context); - return context.Log.DiagnosticVerbosity(); - } } } \ No newline at end of file diff --git a/src/Cake.Core.Tests/Unit/Diagnostics/LogExtensionsTests.Formattable.cs b/src/Cake.Core.Tests/Unit/Diagnostics/LogExtensionsTests.Formattable.cs new file mode 100644 index 0000000000..598838c8d4 --- /dev/null +++ b/src/Cake.Core.Tests/Unit/Diagnostics/LogExtensionsTests.Formattable.cs @@ -0,0 +1,279 @@ +using System; +using Cake.Core.Diagnostics; +using Xunit; + +namespace Cake.Core.Tests.Unit.Diagnostics; + +public partial class LogExtensionsTests +{ + public class Formattable + { + private const string ExpectedMessage = "Hello World"; + private static readonly FormattableString HelloWorld = $"{"Hello"} {"World"}"; + + public class TheDebugMethod + { + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Default_Verbosity() + { + // Given, When + var result = Record.Exception(() => LogExtensions.Debug(null, HelloWorld)); + + // Then + Assert.Null(result); + } + + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Custom_Verbosity() + { + // Given, When + var result = Record.Exception(() => LogExtensions.Debug(null, Verbosity.Normal, HelloWorld)); + + // Then + Assert.Null(result); + } + + [Fact] + public void Can_Write_Debug_Message_With_Default_Verbosity() + { + // Given + var log = new TestLog(); + + // When + log.Debug(HelloWorld); + + // Then + Assert.Equal(Verbosity.Diagnostic, log.Verbosity); + Assert.Equal(LogLevel.Debug, log.Level); + Assert.Equal(ExpectedMessage, log.Message); + } + + [Fact] + public void Can_Write_Debug_Message_With_Custom_Verbosity() + { + // Given + var log = new TestLog(); + + // When + log.Debug(Verbosity.Quiet, HelloWorld); + + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); + Assert.Equal(LogLevel.Debug, log.Level); + Assert.Equal(ExpectedMessage, log.Message); + } + } + + public class TheVerboseMethod + { + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Default_Verbosity() + { + // Given, When + var result = Record.Exception(() => LogExtensions.Verbose(null, HelloWorld)); + + // Then + Assert.Null(result); + } + + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Custom_Verbosity() + { + // Given, When + var result = Record.Exception(() => LogExtensions.Verbose(null, Verbosity.Normal, HelloWorld)); + + // Then + Assert.Null(result); + } + + [Fact] + public void Can_Write_Verbose_Message_With_Default_Verbosity() + { + // Given + var log = new TestLog(); + + // When + log.Verbose(HelloWorld); + + // Then + Assert.Equal(Verbosity.Verbose, log.Verbosity); + Assert.Equal(LogLevel.Verbose, log.Level); + Assert.Equal(ExpectedMessage, log.Message); + } + + [Fact] + public void Can_Write_Verbose_Message_With_Custom_Verbosity() + { + // Given + var log = new TestLog(); + + // When + log.Verbose(Verbosity.Quiet, HelloWorld); + + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); + Assert.Equal(LogLevel.Verbose, log.Level); + Assert.Equal(ExpectedMessage, log.Message); + } + } + + public class TheInformationMethod + { + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Default_Verbosity() + { + // Given, When + var result = Record.Exception(() => LogExtensions.Information(null, HelloWorld)); + + // Then + Assert.Null(result); + } + + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Custom_Verbosity() + { + // Given, When + var result = Record.Exception(() => LogExtensions.Information(null, Verbosity.Normal, HelloWorld)); + + // Then + Assert.Null(result); + } + + [Fact] + public void Can_Write_Informative_Message_With_Default_Verbosity() + { + // Given + var log = new TestLog(); + + // When + log.Information(HelloWorld); + + // Then + Assert.Equal(Verbosity.Normal, log.Verbosity); + Assert.Equal(LogLevel.Information, log.Level); + Assert.Equal(ExpectedMessage, log.Message); + } + + [Fact] + public void Can_Write_Informative_Message_With_Custom_Verbosity() + { + // Given + var log = new TestLog(); + + // When + log.Information(Verbosity.Quiet, HelloWorld); + + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); + Assert.Equal(LogLevel.Information, log.Level); + Assert.Equal(ExpectedMessage, log.Message); + } + } + + public class TheWarningMethod + { + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Default_Verbosity() + { + // Given, When + var result = Record.Exception(() => LogExtensions.Warning(null, HelloWorld)); + + // Then + Assert.Null(result); + } + + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Custom_Verbosity() + { + // Given, When + var result = Record.Exception(() => LogExtensions.Warning(null, Verbosity.Normal, HelloWorld)); + + // Then + Assert.Null(result); + } + + [Fact] + public void Can_Write_Warning_Message_With_Default_Verbosity() + { + // Given + var log = new TestLog(); + + // When + log.Warning(HelloWorld); + + // Then + Assert.Equal(Verbosity.Minimal, log.Verbosity); + Assert.Equal(LogLevel.Warning, log.Level); + Assert.Equal(ExpectedMessage, log.Message); + } + + [Fact] + public void Can_Write_Warning_Message_With_Custom_Verbosity() + { + // Given + var log = new TestLog(); + + // When + log.Warning(Verbosity.Quiet, HelloWorld); + + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); + Assert.Equal(LogLevel.Warning, log.Level); + Assert.Equal(ExpectedMessage, log.Message); + } + } + + public class TheErrorMethod + { + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Default_Verbosity() + { + // Given, When + var result = Record.Exception(() => LogExtensions.Error(null, HelloWorld)); + + // Then + Assert.Null(result); + } + + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Custom_Verbosity() + { + // Given, When + var result = Record.Exception(() => LogExtensions.Error(null, Verbosity.Normal, HelloWorld)); + + // Then + Assert.Null(result); + } + + [Fact] + public void Can_Write_Error_Message_With_Default_Verbosity() + { + // Given + var log = new TestLog(); + + // When + log.Error(HelloWorld); + + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); + Assert.Equal(LogLevel.Error, log.Level); + Assert.Equal(ExpectedMessage, log.Message); + } + + [Fact] + public void Can_Write_Error_Message_With_Custom_Verbosity() + { + // Given + var log = new TestLog(); + + // When + log.Error(Verbosity.Diagnostic, HelloWorld); + + // Then + Assert.Equal(Verbosity.Diagnostic, log.Verbosity); + Assert.Equal(LogLevel.Error, log.Level); + Assert.Equal(ExpectedMessage, log.Message); + } + } + } +} diff --git a/src/Cake.Core.Tests/Unit/Diagnostics/LogExtensionsTests.cs b/src/Cake.Core.Tests/Unit/Diagnostics/LogExtensionsTests.cs index 9d526c69d0..bf5c1bc550 100644 --- a/src/Cake.Core.Tests/Unit/Diagnostics/LogExtensionsTests.cs +++ b/src/Cake.Core.Tests/Unit/Diagnostics/LogExtensionsTests.cs @@ -5,511 +5,510 @@ using Cake.Core.Diagnostics; using Xunit; -namespace Cake.Core.Tests.Unit.Diagnostics +namespace Cake.Core.Tests.Unit.Diagnostics; + +public partial class LogExtensionsTests { - public class LogExtensionsTests + private sealed class TestLog : ICakeLog { - private sealed class TestLog : ICakeLog - { - public Verbosity Verbosity { get; set; } + public Verbosity Verbosity { get; set; } - public LogLevel Level { get; private set; } + public LogLevel Level { get; private set; } - public string Message { get; private set; } + public string Message { get; private set; } - public void Write(Verbosity verbosity, LogLevel level, string format, params object[] args) - { - Verbosity = verbosity; - Level = level; - Message = string.Format(format, args); - } + public void Write(Verbosity verbosity, LogLevel level, string format, params object[] args) + { + Verbosity = verbosity; + Level = level; + Message = string.Format(format, args); } + } - public class TheDebugMethod + public class TheDebugMethod + { + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Default_Verbosity() { - [Fact] - public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Default_Verbosity() - { - // Given, When - var result = Record.Exception(() => LogExtensions.Debug(null, "Hello World")); + // Given, When + var result = Record.Exception(() => LogExtensions.Debug(null, "Hello World")); - // Then - Assert.Null(result); - } + // Then + Assert.Null(result); + } - [Fact] - public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Custom_Verbosity() - { - // Given, When - var result = Record.Exception(() => LogExtensions.Debug(null, Verbosity.Normal, "Hello World")); + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Custom_Verbosity() + { + // Given, When + var result = Record.Exception(() => LogExtensions.Debug(null, Verbosity.Normal, "Hello World")); - // Then - Assert.Null(result); - } + // Then + Assert.Null(result); + } - [Fact] - public void Can_Write_Debug_Message_With_Default_Verbosity() - { - // Given - var log = new TestLog(); + [Fact] + public void Can_Write_Debug_Message_With_Default_Verbosity() + { + // Given + var log = new TestLog(); - // When - log.Debug("Hello World"); + // When + log.Debug("Hello World"); - // Then - Assert.Equal(Verbosity.Diagnostic, log.Verbosity); - Assert.Equal(LogLevel.Debug, log.Level); - Assert.Equal("Hello World", log.Message); - } + // Then + Assert.Equal(Verbosity.Diagnostic, log.Verbosity); + Assert.Equal(LogLevel.Debug, log.Level); + Assert.Equal("Hello World", log.Message); + } - [Fact] - public void Can_Write_Debug_Message_With_Custom_Verbosity() - { - // Given - var log = new TestLog(); + [Fact] + public void Can_Write_Debug_Message_With_Custom_Verbosity() + { + // Given + var log = new TestLog(); - // When - log.Debug(Verbosity.Quiet, "Hello World"); + // When + log.Debug(Verbosity.Quiet, "Hello World"); - // Then - Assert.Equal(Verbosity.Quiet, log.Verbosity); - Assert.Equal(LogLevel.Debug, log.Level); - Assert.Equal("Hello World", log.Message); - } + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); + Assert.Equal(LogLevel.Debug, log.Level); + Assert.Equal("Hello World", log.Message); } + } - public class TheVerboseMethod + public class TheVerboseMethod + { + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Default_Verbosity() { - [Fact] - public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Default_Verbosity() - { - // Given, When - var result = Record.Exception(() => LogExtensions.Verbose(null, "Hello World")); + // Given, When + var result = Record.Exception(() => LogExtensions.Verbose(null, "Hello World")); - // Then - Assert.Null(result); - } + // Then + Assert.Null(result); + } - [Fact] - public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Custom_Verbosity() - { - // Given, When - var result = Record.Exception(() => LogExtensions.Verbose(null, Verbosity.Normal, "Hello World")); + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Custom_Verbosity() + { + // Given, When + var result = Record.Exception(() => LogExtensions.Verbose(null, Verbosity.Normal, "Hello World")); - // Then - Assert.Null(result); - } + // Then + Assert.Null(result); + } - [Fact] - public void Can_Write_Verbose_Message_With_Default_Verbosity() - { - // Given - var log = new TestLog(); + [Fact] + public void Can_Write_Verbose_Message_With_Default_Verbosity() + { + // Given + var log = new TestLog(); - // When - log.Verbose("Hello World"); + // When + log.Verbose("Hello World"); - // Then - Assert.Equal(Verbosity.Verbose, log.Verbosity); - Assert.Equal(LogLevel.Verbose, log.Level); - Assert.Equal("Hello World", log.Message); - } + // Then + Assert.Equal(Verbosity.Verbose, log.Verbosity); + Assert.Equal(LogLevel.Verbose, log.Level); + Assert.Equal("Hello World", log.Message); + } - [Fact] - public void Can_Write_Verbose_Message_With_Custom_Verbosity() - { - // Given - var log = new TestLog(); + [Fact] + public void Can_Write_Verbose_Message_With_Custom_Verbosity() + { + // Given + var log = new TestLog(); - // When - log.Verbose(Verbosity.Quiet, "Hello World"); + // When + log.Verbose(Verbosity.Quiet, "Hello World"); - // Then - Assert.Equal(Verbosity.Quiet, log.Verbosity); - Assert.Equal(LogLevel.Verbose, log.Level); - Assert.Equal("Hello World", log.Message); - } + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); + Assert.Equal(LogLevel.Verbose, log.Level); + Assert.Equal("Hello World", log.Message); } + } - public class TheInformationMethod + public class TheInformationMethod + { + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Default_Verbosity() { - [Fact] - public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Default_Verbosity() - { - // Given, When - var result = Record.Exception(() => LogExtensions.Information(null, "Hello World")); + // Given, When + var result = Record.Exception(() => LogExtensions.Information(null, "Hello World")); - // Then - Assert.Null(result); - } + // Then + Assert.Null(result); + } - [Fact] - public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Custom_Verbosity() - { - // Given, When - var result = Record.Exception(() => LogExtensions.Information(null, Verbosity.Normal, "Hello World")); + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Custom_Verbosity() + { + // Given, When + var result = Record.Exception(() => LogExtensions.Information(null, Verbosity.Normal, "Hello World")); - // Then - Assert.Null(result); - } + // Then + Assert.Null(result); + } - [Fact] - public void Can_Write_Informative_Message_With_Default_Verbosity() - { - // Given - var log = new TestLog(); + [Fact] + public void Can_Write_Informative_Message_With_Default_Verbosity() + { + // Given + var log = new TestLog(); - // When - log.Information("Hello World"); + // When + log.Information("Hello World"); - // Then - Assert.Equal(Verbosity.Normal, log.Verbosity); - Assert.Equal(LogLevel.Information, log.Level); - Assert.Equal("Hello World", log.Message); - } + // Then + Assert.Equal(Verbosity.Normal, log.Verbosity); + Assert.Equal(LogLevel.Information, log.Level); + Assert.Equal("Hello World", log.Message); + } - [Fact] - public void Can_Write_Informative_Message_With_Custom_Verbosity() - { - // Given - var log = new TestLog(); + [Fact] + public void Can_Write_Informative_Message_With_Custom_Verbosity() + { + // Given + var log = new TestLog(); - // When - log.Information(Verbosity.Quiet, "Hello World"); + // When + log.Information(Verbosity.Quiet, "Hello World"); - // Then - Assert.Equal(Verbosity.Quiet, log.Verbosity); - Assert.Equal(LogLevel.Information, log.Level); - Assert.Equal("Hello World", log.Message); - } + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); + Assert.Equal(LogLevel.Information, log.Level); + Assert.Equal("Hello World", log.Message); } + } - public class TheWarningMethod + public class TheWarningMethod + { + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Default_Verbosity() { - [Fact] - public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Default_Verbosity() - { - // Given, When - var result = Record.Exception(() => LogExtensions.Warning(null, "Hello World")); + // Given, When + var result = Record.Exception(() => LogExtensions.Warning(null, "Hello World")); - // Then - Assert.Null(result); - } + // Then + Assert.Null(result); + } - [Fact] - public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Custom_Verbosity() - { - // Given, When - var result = Record.Exception(() => LogExtensions.Warning(null, Verbosity.Normal, "Hello World")); + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Custom_Verbosity() + { + // Given, When + var result = Record.Exception(() => LogExtensions.Warning(null, Verbosity.Normal, "Hello World")); - // Then - Assert.Null(result); - } + // Then + Assert.Null(result); + } - [Fact] - public void Can_Write_Warning_Message_With_Default_Verbosity() - { - // Given - var log = new TestLog(); + [Fact] + public void Can_Write_Warning_Message_With_Default_Verbosity() + { + // Given + var log = new TestLog(); - // When - log.Warning("Hello World"); + // When + log.Warning("Hello World"); - // Then - Assert.Equal(Verbosity.Minimal, log.Verbosity); - Assert.Equal(LogLevel.Warning, log.Level); - Assert.Equal("Hello World", log.Message); - } + // Then + Assert.Equal(Verbosity.Minimal, log.Verbosity); + Assert.Equal(LogLevel.Warning, log.Level); + Assert.Equal("Hello World", log.Message); + } - [Fact] - public void Can_Write_Warning_Message_With_Custom_Verbosity() - { - // Given - var log = new TestLog(); + [Fact] + public void Can_Write_Warning_Message_With_Custom_Verbosity() + { + // Given + var log = new TestLog(); - // When - log.Warning(Verbosity.Quiet, "Hello World"); + // When + log.Warning(Verbosity.Quiet, "Hello World"); - // Then - Assert.Equal(Verbosity.Quiet, log.Verbosity); - Assert.Equal(LogLevel.Warning, log.Level); - Assert.Equal("Hello World", log.Message); - } + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); + Assert.Equal(LogLevel.Warning, log.Level); + Assert.Equal("Hello World", log.Message); } + } - public class TheErrorMethod + public class TheErrorMethod + { + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Default_Verbosity() { - [Fact] - public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Default_Verbosity() - { - // Given, When - var result = Record.Exception(() => LogExtensions.Error(null, "Hello World")); + // Given, When + var result = Record.Exception(() => LogExtensions.Error(null, "Hello World")); - // Then - Assert.Null(result); - } + // Then + Assert.Null(result); + } - [Fact] - public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Custom_Verbosity() - { - // Given, When - var result = Record.Exception(() => LogExtensions.Error(null, Verbosity.Normal, "Hello World")); + [Fact] + public void Should_Not_Throw_If_Log_Is_Null_When_Logging_With_Custom_Verbosity() + { + // Given, When + var result = Record.Exception(() => LogExtensions.Error(null, Verbosity.Normal, "Hello World")); - // Then - Assert.Null(result); - } + // Then + Assert.Null(result); + } - [Fact] - public void Can_Write_Error_Message_With_Default_Verbosity() - { - // Given - var log = new TestLog(); + [Fact] + public void Can_Write_Error_Message_With_Default_Verbosity() + { + // Given + var log = new TestLog(); - // When - log.Error("Hello World"); + // When + log.Error("Hello World"); - // Then - Assert.Equal(Verbosity.Quiet, log.Verbosity); - Assert.Equal(LogLevel.Error, log.Level); - Assert.Equal("Hello World", log.Message); - } + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); + Assert.Equal(LogLevel.Error, log.Level); + Assert.Equal("Hello World", log.Message); + } - [Fact] - public void Can_Write_Error_Message_With_Custom_Verbosity() - { - // Given - var log = new TestLog(); + [Fact] + public void Can_Write_Error_Message_With_Custom_Verbosity() + { + // Given + var log = new TestLog(); - // When - log.Error(Verbosity.Diagnostic, "Hello World"); + // When + log.Error(Verbosity.Diagnostic, "Hello World"); - // Then - Assert.Equal(Verbosity.Diagnostic, log.Verbosity); - Assert.Equal(LogLevel.Error, log.Level); - Assert.Equal("Hello World", log.Message); - } + // Then + Assert.Equal(Verbosity.Diagnostic, log.Verbosity); + Assert.Equal(LogLevel.Error, log.Level); + Assert.Equal("Hello World", log.Message); } + } - public sealed class TheQuietVerbosityMethod + public sealed class TheQuietVerbosityMethod + { + [Fact] + public void Should_Throw_If_Log_Null() { - [Fact] - public void Should_Throw_If_Log_Null() - { - // When - var result = Record.Exception(() => LogExtensions.QuietVerbosity(null)); + // When + var result = Record.Exception(() => LogExtensions.QuietVerbosity(null)); - // Then - AssertEx.IsArgumentNullException(result, "log"); - } + // Then + AssertEx.IsArgumentNullException(result, "log"); + } - [Fact] - public void Should_Set_Log_Verbosity_To_Quiet() - { - // When - var log = new TestLog { Verbosity = Verbosity.Verbose }; - log.QuietVerbosity(); + [Fact] + public void Should_Set_Log_Verbosity_To_Quiet() + { + // When + var log = new TestLog { Verbosity = Verbosity.Verbose }; + log.QuietVerbosity(); - // Then - Assert.Equal(Verbosity.Quiet, log.Verbosity); - } + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); + } - [Fact] - public void Should_Return_Disposable_That_Restores_Log_Verbosity() + [Fact] + public void Should_Return_Disposable_That_Restores_Log_Verbosity() + { + // When + var log = new TestLog { Verbosity = Verbosity.Verbose }; + using (log.QuietVerbosity()) { - // When - var log = new TestLog { Verbosity = Verbosity.Verbose }; - using (log.QuietVerbosity()) - { - } - - // Then - Assert.Equal(Verbosity.Verbose, log.Verbosity); } + + // Then + Assert.Equal(Verbosity.Verbose, log.Verbosity); } + } - public sealed class TheMinimalVerbosityMethod + public sealed class TheMinimalVerbosityMethod + { + [Fact] + public void Should_Throw_If_Log_Null() { - [Fact] - public void Should_Throw_If_Log_Null() - { - // When - var result = Record.Exception(() => LogExtensions.MinimalVerbosity(null)); + // When + var result = Record.Exception(() => LogExtensions.MinimalVerbosity(null)); - // Then - AssertEx.IsArgumentNullException(result, "log"); - } + // Then + AssertEx.IsArgumentNullException(result, "log"); + } - [Fact] - public void Should_Set_Log_Verbosity_To_Minimal() - { - // When - var log = new TestLog { Verbosity = Verbosity.Quiet }; - log.MinimalVerbosity(); + [Fact] + public void Should_Set_Log_Verbosity_To_Minimal() + { + // When + var log = new TestLog { Verbosity = Verbosity.Quiet }; + log.MinimalVerbosity(); - // Then - Assert.Equal(Verbosity.Minimal, log.Verbosity); - } + // Then + Assert.Equal(Verbosity.Minimal, log.Verbosity); + } - [Fact] - public void Should_Return_Disposable_That_Restores_Log_Verbosity() + [Fact] + public void Should_Return_Disposable_That_Restores_Log_Verbosity() + { + // When + var log = new TestLog { Verbosity = Verbosity.Quiet }; + using (log.MinimalVerbosity()) { - // When - var log = new TestLog { Verbosity = Verbosity.Quiet }; - using (log.MinimalVerbosity()) - { - } - - // Then - Assert.Equal(Verbosity.Quiet, log.Verbosity); } + + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); } + } - public sealed class TheNormalVerbosityMethod + public sealed class TheNormalVerbosityMethod + { + [Fact] + public void Should_Throw_If_Log_Null() { - [Fact] - public void Should_Throw_If_Log_Null() - { - // When - var result = Record.Exception(() => LogExtensions.NormalVerbosity(null)); + // When + var result = Record.Exception(() => LogExtensions.NormalVerbosity(null)); - // Then - AssertEx.IsArgumentNullException(result, "log"); - } + // Then + AssertEx.IsArgumentNullException(result, "log"); + } - [Fact] - public void Should_Set_Log_Verbosity_To_Normal() - { - // When - var log = new TestLog { Verbosity = Verbosity.Quiet }; - log.NormalVerbosity(); + [Fact] + public void Should_Set_Log_Verbosity_To_Normal() + { + // When + var log = new TestLog { Verbosity = Verbosity.Quiet }; + log.NormalVerbosity(); - // Then - Assert.Equal(Verbosity.Normal, log.Verbosity); - } + // Then + Assert.Equal(Verbosity.Normal, log.Verbosity); + } - [Fact] - public void Should_Return_Disposable_That_Restores_Log_Verbosity() + [Fact] + public void Should_Return_Disposable_That_Restores_Log_Verbosity() + { + // When + var log = new TestLog { Verbosity = Verbosity.Quiet }; + using (log.NormalVerbosity()) { - // When - var log = new TestLog { Verbosity = Verbosity.Quiet }; - using (log.NormalVerbosity()) - { - } - - // Then - Assert.Equal(Verbosity.Quiet, log.Verbosity); } + + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); } + } - public sealed class TheVerboseVerbosityMethod + public sealed class TheVerboseVerbosityMethod + { + [Fact] + public void Should_Throw_If_Log_Null() { - [Fact] - public void Should_Throw_If_Log_Null() - { - // When - var result = Record.Exception(() => LogExtensions.VerboseVerbosity(null)); + // When + var result = Record.Exception(() => LogExtensions.VerboseVerbosity(null)); - // Then - AssertEx.IsArgumentNullException(result, "log"); - } + // Then + AssertEx.IsArgumentNullException(result, "log"); + } - [Fact] - public void Should_Set_Log_Verbosity_To_Verbose() - { - // When - var log = new TestLog { Verbosity = Verbosity.Quiet }; - log.VerboseVerbosity(); + [Fact] + public void Should_Set_Log_Verbosity_To_Verbose() + { + // When + var log = new TestLog { Verbosity = Verbosity.Quiet }; + log.VerboseVerbosity(); - // Then - Assert.Equal(Verbosity.Verbose, log.Verbosity); - } + // Then + Assert.Equal(Verbosity.Verbose, log.Verbosity); + } - [Fact] - public void Should_Return_Disposable_That_Restores_Log_Verbosity() + [Fact] + public void Should_Return_Disposable_That_Restores_Log_Verbosity() + { + // When + var log = new TestLog { Verbosity = Verbosity.Quiet }; + using (log.VerboseVerbosity()) { - // When - var log = new TestLog { Verbosity = Verbosity.Quiet }; - using (log.VerboseVerbosity()) - { - } - - // Then - Assert.Equal(Verbosity.Quiet, log.Verbosity); } + + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); } + } - public sealed class TheDiagnosticVerbosityMethod + public sealed class TheDiagnosticVerbosityMethod + { + [Fact] + public void Should_Throw_If_Log_Null() { - [Fact] - public void Should_Throw_If_Log_Null() - { - // When - var result = Record.Exception(() => LogExtensions.DiagnosticVerbosity(null)); + // When + var result = Record.Exception(() => LogExtensions.DiagnosticVerbosity(null)); - // Then - AssertEx.IsArgumentNullException(result, "log"); - } + // Then + AssertEx.IsArgumentNullException(result, "log"); + } - [Fact] - public void Should_Set_Log_Verbosity_To_Diagnostic() - { - // When - var log = new TestLog { Verbosity = Verbosity.Quiet }; - log.DiagnosticVerbosity(); + [Fact] + public void Should_Set_Log_Verbosity_To_Diagnostic() + { + // When + var log = new TestLog { Verbosity = Verbosity.Quiet }; + log.DiagnosticVerbosity(); - // Then - Assert.Equal(Verbosity.Diagnostic, log.Verbosity); - } + // Then + Assert.Equal(Verbosity.Diagnostic, log.Verbosity); + } - [Fact] - public void Should_Return_Disposable_That_Restores_Log_Verbosity() + [Fact] + public void Should_Return_Disposable_That_Restores_Log_Verbosity() + { + // When + var log = new TestLog { Verbosity = Verbosity.Quiet }; + using (log.DiagnosticVerbosity()) { - // When - var log = new TestLog { Verbosity = Verbosity.Quiet }; - using (log.DiagnosticVerbosity()) - { - } - - // Then - Assert.Equal(Verbosity.Quiet, log.Verbosity); } + + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); } + } - public sealed class TheWithVerbosityMethod + public sealed class TheWithVerbosityMethod + { + [Fact] + public void Should_Throw_If_Log_Null() { - [Fact] - public void Should_Throw_If_Log_Null() - { - // When - var result = Record.Exception(() => LogExtensions.WithVerbosity(null, Verbosity.Diagnostic)); + // When + var result = Record.Exception(() => LogExtensions.WithVerbosity(null, Verbosity.Diagnostic)); - // Then - AssertEx.IsArgumentNullException(result, "log"); - } + // Then + AssertEx.IsArgumentNullException(result, "log"); + } - [Fact] - public void Should_Set_Log_Verbosity_As_Specified() - { - // When - var log = new TestLog { Verbosity = Verbosity.Quiet }; - log.WithVerbosity(Verbosity.Diagnostic); + [Fact] + public void Should_Set_Log_Verbosity_As_Specified() + { + // When + var log = new TestLog { Verbosity = Verbosity.Quiet }; + log.WithVerbosity(Verbosity.Diagnostic); - // Then - Assert.Equal(Verbosity.Diagnostic, log.Verbosity); - } + // Then + Assert.Equal(Verbosity.Diagnostic, log.Verbosity); + } - [Fact] - public void Should_Return_Disposable_That_Restores_Log_Verbosity() + [Fact] + public void Should_Return_Disposable_That_Restores_Log_Verbosity() + { + // When + var log = new TestLog { Verbosity = Verbosity.Quiet }; + using (log.WithVerbosity(Verbosity.Diagnostic)) { - // When - var log = new TestLog { Verbosity = Verbosity.Quiet }; - using (log.WithVerbosity(Verbosity.Diagnostic)) - { - } - - // Then - Assert.Equal(Verbosity.Quiet, log.Verbosity); } + + // Then + Assert.Equal(Verbosity.Quiet, log.Verbosity); } } } \ No newline at end of file diff --git a/src/Cake.Core/Diagnostics/LogAction.cs b/src/Cake.Core/Diagnostics/LogAction.cs index 207535e4f7..65a8c2a98f 100644 --- a/src/Cake.Core/Diagnostics/LogAction.cs +++ b/src/Cake.Core/Diagnostics/LogAction.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; + namespace Cake.Core.Diagnostics { /// @@ -16,4 +18,16 @@ namespace Cake.Core.Diagnostics /// A composite format string. /// An array of objects to write using format. public delegate void LogActionEntry(string format, params object[] args); + + /// + /// Delegate representing lazy formattable log action. + /// + /// Proxy to log. + public delegate void FormattableLogAction(FormattableLogActionEntry actionEntry); + + /// + /// Delegate representing lazy formattable log entry. + /// + /// The string to be formatted. + public delegate void FormattableLogActionEntry(FormattableString formattable); } \ No newline at end of file diff --git a/src/Cake.Core/Diagnostics/LogExtensions.Disposable.cs b/src/Cake.Core/Diagnostics/LogExtensions.Disposable.cs new file mode 100644 index 0000000000..72a1fc0af8 --- /dev/null +++ b/src/Cake.Core/Diagnostics/LogExtensions.Disposable.cs @@ -0,0 +1,77 @@ +// 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. + +using System; + +namespace Cake.Core.Diagnostics; + +/// +/// Contains extension methods for . +/// +public static partial class LogExtensions +{ + /// + /// Sets the log verbosity to quiet and returns a disposable that restores the log verbosity on dispose. + /// + /// The log. + /// A disposable that restores the log verbosity. + public static IDisposable QuietVerbosity(this ICakeLog log) + { + return log.WithVerbosity(Verbosity.Quiet); + } + + /// + /// Sets the log verbosity to minimal and returns a disposable that restores the log verbosity on dispose. + /// + /// The log. + /// A disposable that restores the log verbosity. + public static IDisposable MinimalVerbosity(this ICakeLog log) + { + return log.WithVerbosity(Verbosity.Minimal); + } + + /// + /// Sets the log verbosity to normal and returns a disposable that restores the log verbosity on dispose. + /// + /// The log. + /// A disposable that restores the log verbosity. + public static IDisposable NormalVerbosity(this ICakeLog log) + { + return log.WithVerbosity(Verbosity.Normal); + } + + /// + /// Sets the log verbosity to verbose and returns a disposable that restores the log verbosity on dispose. + /// + /// The log. + /// A disposable that restores the log verbosity. + public static IDisposable VerboseVerbosity(this ICakeLog log) + { + return log.WithVerbosity(Verbosity.Verbose); + } + + /// + /// Sets the log verbosity to diagnostic and returns a disposable that restores the log verbosity on dispose. + /// + /// The log. + /// A disposable that restores the log verbosity. + public static IDisposable DiagnosticVerbosity(this ICakeLog log) + { + return log.WithVerbosity(Verbosity.Diagnostic); + } + + /// + /// Sets the log verbosity as specified and returns a disposable that restores the log verbosity on dispose. + /// + /// The log. + /// The verbosity. + /// A disposable that restores the log verbosity. + public static IDisposable WithVerbosity(this ICakeLog log, Verbosity verbosity) + { + ArgumentNullException.ThrowIfNull(log); + var lastVerbosity = log.Verbosity; + log.Verbosity = verbosity; + return Disposable.Create(() => log.Verbosity = lastVerbosity); + } +} \ No newline at end of file diff --git a/src/Cake.Core/Diagnostics/LogExtensions.Formattable.cs b/src/Cake.Core/Diagnostics/LogExtensions.Formattable.cs new file mode 100644 index 0000000000..90cbbf8f78 --- /dev/null +++ b/src/Cake.Core/Diagnostics/LogExtensions.Formattable.cs @@ -0,0 +1,272 @@ +// 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. + +using System; + +namespace Cake.Core.Diagnostics; + +/// +/// Contains extension methods for . +/// +public static partial class LogExtensions +{ + /// + /// Writes the text representation of the formattable string to the + /// log using the specified verbosity, log level and format information. + /// + /// The log. + /// The verbosity. + /// The log level. + /// The string to be formatted. + public static void Write(this ICakeLog log, Verbosity verbosity, LogLevel level, FormattableString formattable) + { + log?.Write(verbosity, level, formattable.Format, formattable.GetArguments()); + } + + /// + /// Writes an formattable string error message to the log using the specified format information. + /// + /// The log. + /// The string to be formatted. + public static void Error(this ICakeLog log, FormattableString formattable) + { + Error(log, Verbosity.Quiet, formattable); + } + + /// + /// Writes an formattable string error message to the log using the specified format information. + /// + /// The log. + /// The verbosity. + /// The string to be formatted. + public static void Error(this ICakeLog log, Verbosity verbosity, FormattableString formattable) + { + log?.Write(verbosity, LogLevel.Error, formattable); + } + + /// + /// Writes an formattable string error message to the log using the specified verbosity and log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The verbosity. + /// The formattable log action. + public static void Error(this ICakeLog log, Verbosity verbosity, FormattableLogAction formattableLogAction) + { + Write(log, verbosity, LogLevel.Error, formattableLogAction); + } + + /// + /// Writes an formattable string error message to the log using the specified log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The log action. + public static void Error(this ICakeLog log, FormattableLogAction formattableLogAction) + { + Error(log, Verbosity.Quiet, formattableLogAction); + } + + /// + /// Writes a formattable string warning message to the log using the specified format information. + /// + /// The log. + /// The string to be formatted. + public static void Warning(this ICakeLog log, FormattableString formattable) + { + Warning(log, Verbosity.Minimal, formattable); + } + + /// + /// Writes a formattable string warning message to the log using the specified verbosity and format information. + /// + /// The log. + /// The verbosity. + /// The string to be formatted. + public static void Warning(this ICakeLog log, Verbosity verbosity, FormattableString formattable) + { + log?.Write(verbosity, LogLevel.Warning, formattable); + } + + /// + /// Writes a formattable string warning message to the log using the specified log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The formattable log action. + public static void Warning(this ICakeLog log, FormattableLogAction formattableLogAction) + { + Warning(log, Verbosity.Minimal, formattableLogAction); + } + + /// + /// Writes a formattable string warning message to the log using the specified verbosity and log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The verbosity. + /// The formattable log action. + public static void Warning(this ICakeLog log, Verbosity verbosity, FormattableLogAction formattableLogAction) + { + Write(log, verbosity, LogLevel.Warning, formattableLogAction); + } + + /// + /// Writes an formattable string informational message to the log using the specified format information. + /// + /// The log. + /// The string to be formatted. + public static void Information(this ICakeLog log, FormattableString formattable) + { + Information(log, Verbosity.Normal, formattable); + } + + /// + /// Writes an formattable string informational message to the log using the specified verbosity and format information. + /// + /// The log. + /// The verbosity. + /// The string to be formatted. + public static void Information(this ICakeLog log, Verbosity verbosity, FormattableString formattable) + { + log?.Write(verbosity, LogLevel.Information, formattable); + } + + /// + /// Writes an formattable string informational message to the log using the specified verbosity and log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The verbosity. + /// The formattable log action. + public static void Information(this ICakeLog log, Verbosity verbosity, FormattableLogAction formattableLogAction) + { + Write(log, verbosity, LogLevel.Information, formattableLogAction); + } + + /// + /// Writes an formattable string informational message to the log using the specified log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The formattable log action. + public static void Information(this ICakeLog log, FormattableLogAction formattableLogAction) + { + Information(log, Verbosity.Normal, formattableLogAction); + } + + /// + /// Writes a formattable string verbose message to the log using the specified format information. + /// + /// The log. + /// The string to be formatted. + public static void Verbose(this ICakeLog log, FormattableString formattable) + { + Verbose(log, Verbosity.Verbose, formattable); + } + + /// + /// Writes a formattable string verbose message to the log using the specified verbosity and format information. + /// + /// The log. + /// The verbosity. + /// The string to be formatted. + public static void Verbose(this ICakeLog log, Verbosity verbosity, FormattableString formattable) + { + log?.Write(verbosity, LogLevel.Verbose, formattable); + } + + /// + /// Writes a formattable string verbose message to the log using the specified log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The formattable log action. + public static void Verbose(this ICakeLog log, FormattableLogAction formattableLogAction) + { + Verbose(log, Verbosity.Verbose, formattableLogAction); + } + + /// + /// Writes a formattable string verbose message to the log using the specified verbosity and log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The verbosity. + /// The formattable log action. + public static void Verbose(this ICakeLog log, Verbosity verbosity, FormattableLogAction formattableLogAction) + { + Write(log, verbosity, LogLevel.Verbose, formattableLogAction); + } + + /// + /// Writes a formattable string debug message to the log using the specified format information. + /// + /// The log. + /// The string to be formatted. + public static void Debug(this ICakeLog log, FormattableString formattable) + { + Debug(log, Verbosity.Diagnostic, formattable); + } + + /// + /// Writes a formattable string debug message to the log using the specified verbosity and format information. + /// + /// The log. + /// The verbosity. + /// The string to be formatted. + public static void Debug(this ICakeLog log, Verbosity verbosity, FormattableString formattable) + { + log?.Write(verbosity, LogLevel.Debug, formattable); + } + + /// + /// Writes a formattable string debug message to the log using the specified log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The formattable log action. + public static void Debug(this ICakeLog log, FormattableLogAction formattableLogAction) + { + Debug(log, Verbosity.Diagnostic, formattableLogAction); + } + + /// + /// Writes a formattable string debug message to the log using the specified verbosity and log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The verbosity. + /// The formattable log action. + public static void Debug(this ICakeLog log, Verbosity verbosity, FormattableLogAction formattableLogAction) + { + Write(log, verbosity, LogLevel.Debug, formattableLogAction); + } + + /// + /// Writes a formattable string message to the log using the specified verbosity, log level and log action delegate. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The verbosity. + /// The log level. + /// The log action. + public static void Write(this ICakeLog log, Verbosity verbosity, LogLevel level, FormattableLogAction formattableLogAction) + { + if (log == null || formattableLogAction == null) + { + return; + } + + if (verbosity > log.Verbosity) + { + return; + } + + void actionEntry(FormattableString formattable) + => log.Write(verbosity, level, formattable); + + formattableLogAction(actionEntry); + } +} diff --git a/src/Cake.Core/Diagnostics/LogExtensions.cs b/src/Cake.Core/Diagnostics/LogExtensions.cs index b22e3ee85a..58f80c32b7 100644 --- a/src/Cake.Core/Diagnostics/LogExtensions.cs +++ b/src/Cake.Core/Diagnostics/LogExtensions.cs @@ -2,431 +2,366 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; +namespace Cake.Core.Diagnostics; -namespace Cake.Core.Diagnostics +/// +/// Contains extension methods for . +/// +public static partial class LogExtensions { /// - /// Contains extension methods for . + /// Writes an error message to the log using the specified format information. /// - public static class LogExtensions + /// The log. + /// A composite format string. + /// An array of objects to write using format. + public static void Error(this ICakeLog log, string format, params object[] args) { - /// - /// Writes an error message to the log using the specified format information. - /// - /// The log. - /// A composite format string. - /// An array of objects to write using format. - public static void Error(this ICakeLog log, string format, params object[] args) - { - Error(log, Verbosity.Quiet, format, args); - } - - /// - /// Writes an error message to the log using the specified verbosity and format information. - /// - /// The log. - /// The verbosity. - /// A composite format string. - /// An array of objects to write using format. - public static void Error(this ICakeLog log, Verbosity verbosity, string format, params object[] args) - { - log?.Write(verbosity, LogLevel.Error, format, args); - } - - /// - /// Writes an error message to the log using the specified verbosity and log message action. - /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. - /// - /// The log. - /// The verbosity. - /// The log action. - public static void Error(this ICakeLog log, Verbosity verbosity, LogAction logAction) - { - Write(log, verbosity, LogLevel.Error, logAction); - } - - /// - /// Writes an error message to the log using the specified log message action. - /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. - /// - /// The log. - /// The log action. - public static void Error(this ICakeLog log, LogAction logAction) - { - Error(log, Verbosity.Quiet, logAction); - } + Error(log, Verbosity.Quiet, format, args); + } - /// - /// Writes an error message to the log using the specified value. - /// - /// The log. - /// The value. - public static void Error(this ICakeLog log, object value) - { - log.Error("{0}", value); - } + /// + /// Writes an error message to the log using the specified verbosity and format information. + /// + /// The log. + /// The verbosity. + /// A composite format string. + /// An array of objects to write using format. + public static void Error(this ICakeLog log, Verbosity verbosity, string format, params object[] args) + { + log?.Write(verbosity, LogLevel.Error, format, args); + } - /// - /// Writes an error message to the log using the specified string value. - /// - /// The log. - /// The value. - public static void Error(this ICakeLog log, string value) - { - log.Error("{0}", value); - } + /// + /// Writes an error message to the log using the specified verbosity and log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The verbosity. + /// The log action. + public static void Error(this ICakeLog log, Verbosity verbosity, LogAction logAction) + { + Write(log, verbosity, LogLevel.Error, logAction); + } - /// - /// Writes a warning message to the log using the specified format information. - /// - /// The log. - /// A composite format string. - /// An array of objects to write using format. - public static void Warning(this ICakeLog log, string format, params object[] args) - { - Warning(log, Verbosity.Minimal, format, args); - } + /// + /// Writes an error message to the log using the specified log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The log action. + public static void Error(this ICakeLog log, LogAction logAction) + { + Error(log, Verbosity.Quiet, logAction); + } - /// - /// Writes a warning message to the log using the specified verbosity and format information. - /// - /// The log. - /// The verbosity. - /// A composite format string. - /// An array of objects to write using format. - public static void Warning(this ICakeLog log, Verbosity verbosity, string format, params object[] args) - { - log?.Write(verbosity, LogLevel.Warning, format, args); - } + /// + /// Writes an error message to the log using the specified value. + /// + /// The log. + /// The value. + public static void Error(this ICakeLog log, object value) + { + log.Error("{0}", value); + } - /// - /// Writes a warning message to the log using the specified log message action. - /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. - /// - /// The log. - /// The log action. - public static void Warning(this ICakeLog log, LogAction logAction) - { - Warning(log, Verbosity.Minimal, logAction); - } + /// + /// Writes an error message to the log using the specified string value. + /// + /// The log. + /// The value. + public static void Error(this ICakeLog log, string value) + { + log.Error("{0}", value); + } - /// - /// Writes a warning message to the log using the specified verbosity and log message action. - /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. - /// - /// The log. - /// The verbosity. - /// The log action. - public static void Warning(this ICakeLog log, Verbosity verbosity, LogAction logAction) - { - Write(log, verbosity, LogLevel.Warning, logAction); - } + /// + /// Writes a warning message to the log using the specified format information. + /// + /// The log. + /// A composite format string. + /// An array of objects to write using format. + public static void Warning(this ICakeLog log, string format, params object[] args) + { + Warning(log, Verbosity.Minimal, format, args); + } - /// - /// Writes an warning message to the log using the specified value. - /// - /// The log. - /// The value. - public static void Warning(this ICakeLog log, object value) - { - log.Warning("{0}", value); - } + /// + /// Writes a warning message to the log using the specified verbosity and format information. + /// + /// The log. + /// The verbosity. + /// A composite format string. + /// An array of objects to write using format. + public static void Warning(this ICakeLog log, Verbosity verbosity, string format, params object[] args) + { + log?.Write(verbosity, LogLevel.Warning, format, args); + } - /// - /// Writes an warning message to the log using the specified string value. - /// - /// The log. - /// The value. - public static void Warning(this ICakeLog log, string value) - { - log.Warning("{0}", value); - } + /// + /// Writes a warning message to the log using the specified log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The log action. + public static void Warning(this ICakeLog log, LogAction logAction) + { + Warning(log, Verbosity.Minimal, logAction); + } - /// - /// Writes an informational message to the log using the specified format information. - /// - /// The log. - /// A composite format string. - /// An array of objects to write using format. - public static void Information(this ICakeLog log, string format, params object[] args) - { - Information(log, Verbosity.Normal, format, args); - } + /// + /// Writes a warning message to the log using the specified verbosity and log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The verbosity. + /// The log action. + public static void Warning(this ICakeLog log, Verbosity verbosity, LogAction logAction) + { + Write(log, verbosity, LogLevel.Warning, logAction); + } - /// - /// Writes an informational message to the log using the specified verbosity and format information. - /// - /// The log. - /// The verbosity. - /// A composite format string. - /// An array of objects to write using format. - public static void Information(this ICakeLog log, Verbosity verbosity, string format, params object[] args) - { - log?.Write(verbosity, LogLevel.Information, format, args); - } + /// + /// Writes an warning message to the log using the specified value. + /// + /// The log. + /// The value. + public static void Warning(this ICakeLog log, object value) + { + log.Warning("{0}", value); + } - /// - /// Writes an informational message to the log using the specified verbosity and log message action. - /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. - /// - /// The log. - /// The verbosity. - /// The log action. - public static void Information(this ICakeLog log, Verbosity verbosity, LogAction logAction) - { - Write(log, verbosity, LogLevel.Information, logAction); - } + /// + /// Writes an warning message to the log using the specified string value. + /// + /// The log. + /// The value. + public static void Warning(this ICakeLog log, string value) + { + log.Warning("{0}", value); + } - /// - /// Writes an informational message to the log using the specified log message action. - /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. - /// - /// The log. - /// The log action. - public static void Information(this ICakeLog log, LogAction logAction) - { - Information(log, Verbosity.Normal, logAction); - } + /// + /// Writes an informational message to the log using the specified format information. + /// + /// The log. + /// A composite format string. + /// An array of objects to write using format. + public static void Information(this ICakeLog log, string format, params object[] args) + { + Information(log, Verbosity.Normal, format, args); + } - /// - /// Writes an informational message to the log using the specified value. - /// - /// The log. - /// The value. - public static void Information(this ICakeLog log, object value) - { - log.Information("{0}", value); - } + /// + /// Writes an informational message to the log using the specified verbosity and format information. + /// + /// The log. + /// The verbosity. + /// A composite format string. + /// An array of objects to write using format. + public static void Information(this ICakeLog log, Verbosity verbosity, string format, params object[] args) + { + log?.Write(verbosity, LogLevel.Information, format, args); + } - /// - /// Writes an informational message to the log using the specified string value. - /// - /// The log. - /// The value. - public static void Information(this ICakeLog log, string value) - { - log.Information("{0}", value); - } + /// + /// Writes an informational message to the log using the specified verbosity and log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The verbosity. + /// The log action. + public static void Information(this ICakeLog log, Verbosity verbosity, LogAction logAction) + { + Write(log, verbosity, LogLevel.Information, logAction); + } - /// - /// Writes a verbose message to the log using the specified format information. - /// - /// The log. - /// A composite format string. - /// An array of objects to write using format. - public static void Verbose(this ICakeLog log, string format, params object[] args) - { - Verbose(log, Verbosity.Verbose, format, args); - } + /// + /// Writes an informational message to the log using the specified log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The log action. + public static void Information(this ICakeLog log, LogAction logAction) + { + Information(log, Verbosity.Normal, logAction); + } - /// - /// Writes a verbose message to the log using the specified verbosity and format information. - /// - /// The log. - /// The verbosity. - /// A composite format string. - /// An array of objects to write using format. - public static void Verbose(this ICakeLog log, Verbosity verbosity, string format, params object[] args) - { - log?.Write(verbosity, LogLevel.Verbose, format, args); - } + /// + /// Writes an informational message to the log using the specified value. + /// + /// The log. + /// The value. + public static void Information(this ICakeLog log, object value) + { + log.Information("{0}", value); + } - /// - /// Writes a verbose message to the log using the specified log message action. - /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. - /// - /// The log. - /// The log action. - public static void Verbose(this ICakeLog log, LogAction logAction) - { - Verbose(log, Verbosity.Verbose, logAction); - } + /// + /// Writes an informational message to the log using the specified string value. + /// + /// The log. + /// The value. + public static void Information(this ICakeLog log, string value) + { + log.Information("{0}", value); + } - /// - /// Writes a verbose message to the log using the specified verbosity and log message action. - /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. - /// - /// The log. - /// The verbosity. - /// The log action. - public static void Verbose(this ICakeLog log, Verbosity verbosity, LogAction logAction) - { - Write(log, verbosity, LogLevel.Verbose, logAction); - } + /// + /// Writes a verbose message to the log using the specified format information. + /// + /// The log. + /// A composite format string. + /// An array of objects to write using format. + public static void Verbose(this ICakeLog log, string format, params object[] args) + { + Verbose(log, Verbosity.Verbose, format, args); + } - /// - /// Writes a verbose message to the log using the specified value. - /// - /// The log. - /// The value. - public static void Verbose(this ICakeLog log, object value) - { - log.Verbose("{0}", value); - } + /// + /// Writes a verbose message to the log using the specified verbosity and format information. + /// + /// The log. + /// The verbosity. + /// A composite format string. + /// An array of objects to write using format. + public static void Verbose(this ICakeLog log, Verbosity verbosity, string format, params object[] args) + { + log?.Write(verbosity, LogLevel.Verbose, format, args); + } - /// - /// Writes a verbose message to the log using the specified string value. - /// - /// The log. - /// The value. - public static void Verbose(this ICakeLog log, string value) - { - log.Verbose("{0}", value); - } + /// + /// Writes a verbose message to the log using the specified log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The log action. + public static void Verbose(this ICakeLog log, LogAction logAction) + { + Verbose(log, Verbosity.Verbose, logAction); + } - /// - /// Writes a debug message to the log using the specified format information. - /// - /// The log. - /// A composite format string. - /// An array of objects to write using format. - public static void Debug(this ICakeLog log, string format, params object[] args) - { - Debug(log, Verbosity.Diagnostic, format, args); - } + /// + /// Writes a verbose message to the log using the specified verbosity and log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The verbosity. + /// The log action. + public static void Verbose(this ICakeLog log, Verbosity verbosity, LogAction logAction) + { + Write(log, verbosity, LogLevel.Verbose, logAction); + } - /// - /// Writes a debug message to the log using the specified verbosity and format information. - /// - /// The log. - /// The verbosity. - /// A composite format string. - /// An array of objects to write using format. - public static void Debug(this ICakeLog log, Verbosity verbosity, string format, params object[] args) - { - log?.Write(verbosity, LogLevel.Debug, format, args); - } + /// + /// Writes a verbose message to the log using the specified value. + /// + /// The log. + /// The value. + public static void Verbose(this ICakeLog log, object value) + { + log.Verbose("{0}", value); + } - /// - /// Writes a debug message to the log using the specified log message action. - /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. - /// - /// The log. - /// The log action. - public static void Debug(this ICakeLog log, LogAction logAction) - { - Debug(log, Verbosity.Diagnostic, logAction); - } + /// + /// Writes a verbose message to the log using the specified string value. + /// + /// The log. + /// The value. + public static void Verbose(this ICakeLog log, string value) + { + log.Verbose("{0}", value); + } - /// - /// Writes a debug message to the log using the specified verbosity and log message action. - /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. - /// - /// The log. - /// The verbosity. - /// The log action. - public static void Debug(this ICakeLog log, Verbosity verbosity, LogAction logAction) - { - Write(log, verbosity, LogLevel.Debug, logAction); - } + /// + /// Writes a debug message to the log using the specified format information. + /// + /// The log. + /// A composite format string. + /// An array of objects to write using format. + public static void Debug(this ICakeLog log, string format, params object[] args) + { + Debug(log, Verbosity.Diagnostic, format, args); + } - /// - /// Writes a debug message to the log using the specified value. - /// - /// The log. - /// The value. - public static void Debug(this ICakeLog log, object value) - { - log.Debug("{0}", value); - } + /// + /// Writes a debug message to the log using the specified verbosity and format information. + /// + /// The log. + /// The verbosity. + /// A composite format string. + /// An array of objects to write using format. + public static void Debug(this ICakeLog log, Verbosity verbosity, string format, params object[] args) + { + log?.Write(verbosity, LogLevel.Debug, format, args); + } - /// - /// Writes a debug message to the log using the specified string value. - /// - /// The log. - /// The value. - public static void Debug(this ICakeLog log, string value) - { - log.Debug("{0}", value); - } + /// + /// Writes a debug message to the log using the specified log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The log action. + public static void Debug(this ICakeLog log, LogAction logAction) + { + Debug(log, Verbosity.Diagnostic, logAction); + } - /// - /// Writes a message to the log using the specified verbosity, log level and log action delegate. - /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. - /// - /// The log. - /// The verbosity. - /// The log level. - /// The log action. - public static void Write(this ICakeLog log, Verbosity verbosity, LogLevel level, LogAction logAction) - { - if (log == null || logAction == null) - { - return; - } - - if (verbosity > log.Verbosity) - { - return; - } - - LogActionEntry actionEntry = (format, args) => log.Write(verbosity, level, format, args); - logAction(actionEntry); - } + /// + /// Writes a debug message to the log using the specified verbosity and log message action. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The verbosity. + /// The log action. + public static void Debug(this ICakeLog log, Verbosity verbosity, LogAction logAction) + { + Write(log, verbosity, LogLevel.Debug, logAction); + } - /// - /// Sets the log verbosity to quiet and returns a disposable that restores the log verbosity on dispose. - /// - /// The log. - /// A disposable that restores the log verbosity. - public static IDisposable QuietVerbosity(this ICakeLog log) - { - return log.WithVerbosity(Verbosity.Quiet); - } + /// + /// Writes a debug message to the log using the specified value. + /// + /// The log. + /// The value. + public static void Debug(this ICakeLog log, object value) + { + log.Debug("{0}", value); + } - /// - /// Sets the log verbosity to minimal and returns a disposable that restores the log verbosity on dispose. - /// - /// The log. - /// A disposable that restores the log verbosity. - public static IDisposable MinimalVerbosity(this ICakeLog log) - { - return log.WithVerbosity(Verbosity.Minimal); - } + /// + /// Writes a debug message to the log using the specified string value. + /// + /// The log. + /// The value. + public static void Debug(this ICakeLog log, string value) + { + log.Debug("{0}", value); + } - /// - /// Sets the log verbosity to normal and returns a disposable that restores the log verbosity on dispose. - /// - /// The log. - /// A disposable that restores the log verbosity. - public static IDisposable NormalVerbosity(this ICakeLog log) + /// + /// Writes a message to the log using the specified verbosity, log level and log action delegate. + /// Evaluates log message only if the verbosity is equal to or more verbose than the log's verbosity. + /// + /// The log. + /// The verbosity. + /// The log level. + /// The log action. + public static void Write(this ICakeLog log, Verbosity verbosity, LogLevel level, LogAction logAction) + { + if (log == null || logAction == null) { - return log.WithVerbosity(Verbosity.Normal); + return; } - /// - /// Sets the log verbosity to verbose and returns a disposable that restores the log verbosity on dispose. - /// - /// The log. - /// A disposable that restores the log verbosity. - public static IDisposable VerboseVerbosity(this ICakeLog log) + if (verbosity > log.Verbosity) { - return log.WithVerbosity(Verbosity.Verbose); + return; } - /// - /// Sets the log verbosity to diagnostic and returns a disposable that restores the log verbosity on dispose. - /// - /// The log. - /// A disposable that restores the log verbosity. - public static IDisposable DiagnosticVerbosity(this ICakeLog log) - { - return log.WithVerbosity(Verbosity.Diagnostic); - } + void actionEntry(string format, object[] args) + => log.Write(verbosity, level, format, args); - /// - /// Sets the log verbosity as specified and returns a disposable that restores the log verbosity on dispose. - /// - /// The log. - /// The verbosity. - /// A disposable that restores the log verbosity. - public static IDisposable WithVerbosity(this ICakeLog log, Verbosity verbosity) - { - ArgumentNullException.ThrowIfNull(log); - var lastVerbosity = log.Verbosity; - log.Verbosity = verbosity; - return Disposable.Create(() => log.Verbosity = lastVerbosity); - } + logAction(actionEntry); } } \ No newline at end of file diff --git a/tests/integration/Cake.Common/Diagnostics/LoggingAliases.cake b/tests/integration/Cake.Common/Diagnostics/LoggingAliases.cake index e509117eb1..f47fe52288 100644 --- a/tests/integration/Cake.Common/Diagnostics/LoggingAliases.cake +++ b/tests/integration/Cake.Common/Diagnostics/LoggingAliases.cake @@ -20,6 +20,22 @@ var logActionMethods = new [] { new { Name = "Debug", Action = new Action(Debug), Verbosity = Verbosity.Diagnostic } }; +var logFormattableStringMethods = new [] { + new { Name = "Error", Action = new Action(Error)}, + new { Name = "Warning", Action = new Action(Warning)}, + new { Name = "Information", Action = new Action(Information)}, + new { Name = "Verbose", Action = new Action(Verbose)}, + new { Name = "Debug", Action = new Action(Debug)} +}; + +var logFormattableLogActionMethods = new [] { + new { Name = "Error", Action = new Action(Error), Verbosity = Verbosity.Quiet }, + new { Name = "Warning", Action = new Action(Warning), Verbosity = Verbosity.Minimal }, + new { Name = "Information", Action = new Action(Information), Verbosity = Verbosity.Normal }, + new { Name = "Verbose", Action = new Action(Verbose), Verbosity = Verbosity.Verbose }, + new { Name = "Debug", Action = new Action(Debug), Verbosity = Verbosity.Diagnostic } +}; + var logStringMethods = new [] { new { Name = "Error", Action = new Action(Error)}, new { Name = "Warning", Action = new Action(Warning)}, @@ -47,19 +63,12 @@ Array.ForEach( Task(string.Format("Cake.Common.Diagnostics.LoggingAliases.StringFormat.{0}.{1}", verbosity, logStringFormatMethod.Name)) .Does(() => { - var originalVerbosity = Context.Log.Verbosity; - try + // Given + using (Context.WithVerbosity(verbosity)) { - // Given - Context.Log.Verbosity = verbosity; - // When logStringFormatMethod.Action("Cake.Common.Diagnostics.LoggingAliases.StringFormat.{0}.{1}", new object[] { verbosity, logStringFormatMethod.Name }); } - finally - { - Context.Log.Verbosity = originalVerbosity; - } }).Task.Name )) ); @@ -72,11 +81,9 @@ Array.ForEach( Task(string.Format("Cake.Common.Diagnostics.LoggingAliases.LogAction.{0}.{1}", verbosity, logActionMethod.Name)) .Does(() => { - var originalVerbosity = Context.Log.Verbosity; - try + // Given + using (Context.WithVerbosity(verbosity)) { - // Given - Context.Log.Verbosity = verbosity; bool called = false; LogAction action = entry=>{ called = true; @@ -88,10 +95,6 @@ Array.ForEach( // Then Assert.True(called || verbosity { - var originalVerbosity = Context.Log.Verbosity; - try + // Given + using (Context.WithVerbosity(verbosity)) { - // Given - Context.Log.Verbosity = verbosity; - // When logStringMethod.Action(string.Format("Cake.Common.Diagnostics.LoggingAliases.String.{0}.{1}: {2}", verbosity, logStringMethod.Name, "{Bon}jour")); } - finally - { - Context.Log.Verbosity = originalVerbosity; - } }).Task.Name )) ); @@ -129,18 +125,55 @@ Array.ForEach( Task(string.Format("Cake.Common.Diagnostics.LoggingAliases.Object.{0}.{1}", verbosity, logObjectMethod.Name)) .Does(() => { - var originalVerbosity = Context.Log.Verbosity; - try + // Given + using (Context.WithVerbosity(verbosity)) { - // Given - Context.Log.Verbosity = verbosity; - // When logObjectMethod.Action(new { Test = "Cake.Common.Diagnostics.LoggingAliases.Object", Verbosity = verbosity, Name = logObjectMethod.Name }); } - finally + }).Task.Name + )) +); + +Array.ForEach( + verbosities, + verbosity => Array.ForEach( + logFormattableStringMethods, + logFormattableStringMethod => loggingAliasesTask.IsDependentOn( + Task(string.Format("Cake.Common.Diagnostics.LoggingAliases.FormattableString.{0}.{1}", verbosity, logFormattableStringMethod.Name)) + .Does(() => + { + // Given + using (Context.WithVerbosity(verbosity)) + { + // When + logFormattableStringMethod.Action($"Cake.Common.Diagnostics.LoggingAliases.FormattableString.{verbosity}.{logFormattableStringMethod.Name}: {"Bon"}jour"); + } + }).Task.Name + )) +); + +Array.ForEach( + verbosities, + verbosity => Array.ForEach( + logFormattableLogActionMethods, + logFormattableLogActionMethod => loggingAliasesTask.IsDependentOn( + Task(string.Format("Cake.Common.Diagnostics.LoggingAliases.FormattableLogAction.{0}.{1}", verbosity, logFormattableLogActionMethod.Name)) + .Does(() => + { + // Given + using (Context.WithVerbosity(verbosity)) { - Context.Log.Verbosity = originalVerbosity; + bool called = false; + FormattableLogAction action = entry => { + called = true; + }; + + // When + logFormattableLogActionMethod.Action(action); + + // Then + Assert.True(called || verbosity < logFormattableLogActionMethod.Verbosity); } }).Task.Name ))