Created
November 14, 2022 16:38
-
-
Save ViktorHofer/e67d8e6238c97622f8ac8388dc521f72 to your computer and use it in GitHub Desktop.
Loggers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) .NET Foundation and contributors. All rights reserved. | |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | |
using System; | |
namespace Microsoft.DotNet.SymbolExtensions.Logging | |
{ | |
/// <summary> | |
/// A console based log | |
/// </summary> | |
public class ConsoleLog : ILog | |
{ | |
protected readonly MessageImportance MessageImportance; | |
public ConsoleLog(MessageImportance messageImportance) | |
{ | |
MessageImportance = messageImportance; | |
} | |
/// <inheritdoc /> | |
public void LogError(string code, string format, params string[] args) => | |
Console.Error.WriteLine(code + ": " + string.Format(format, args)); | |
/// <inheritdoc /> | |
public void LogWarning(string code, string format, params string[] args) => | |
Console.WriteLine(code + ": " + string.Format(format, args)); | |
/// <inheritdoc /> | |
public void LogMessage(MessageImportance importance, string format, params string[] args) | |
{ | |
if (importance > MessageImportance) | |
return; | |
Console.WriteLine(format, args); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) .NET Foundation and contributors. All rights reserved. | |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | |
namespace Microsoft.DotNet.SymbolExtensions.Logging | |
{ | |
/// <summary> | |
/// A log interface that is used to emit messages, warnings and errors. | |
/// </summary> | |
public interface ILog | |
{ | |
/// <summary> | |
/// Log an error based on passed in code, format and additional arguments. | |
/// </summary> | |
/// <param name="code">The suppression code</param> | |
/// <param name="format">The message format/param> | |
/// <param name="args">The message format arguments</param> | |
/// <returns>Returns true if the error is logged and not suppressed.</returns> | |
bool LogError(string code, string format, params string[] args); | |
/// <summary> | |
/// Log a warning based on passed in code, format and additional arguments. | |
/// </summary> | |
/// <param name="code">The suppression code</param> | |
/// <param name="format">The message format/param> | |
/// <param name="args">The message format arguments</param> | |
/// <returns>Returns true if the warning is logged and not suppressed.</returns> | |
bool LogWarning(string code, string format, params string[] args); | |
/// <summary> | |
/// Log a message based on the passed in importance, format and arguments. | |
/// </summary> | |
/// <param name="importance">The message importance</param> | |
/// <param name="format">The message format/param> | |
/// <param name="args">The message format arguments</param> | |
void LogMessage(MessageImportance importance, string format, params string[] args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) .NET Foundation and contributors. All rights reserved. | |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | |
using Microsoft.DotNet.SymbolExtensions.Logging; | |
namespace Microsoft.DotNet.ApiCompatibility | |
{ | |
/// <summary> | |
/// A log interface that is used to emit messages and suppressable warnings and errors. | |
/// </summary> | |
public interface ISuppressableLog : ILog | |
{ | |
/// <summary> | |
/// Log an error based on a passed in suppression, code, format and additional arguments. | |
/// </summary> | |
/// <param name="suppression">The suppression object which contains the rule information.</param> | |
/// <param name="code">The suppression code</param> | |
/// <param name="format">The message format/param> | |
/// <param name="args">The message format arguments</param> | |
/// <returns>Returns true if the error is logged and not suppressed.</returns> | |
bool LogError(Suppression suppression, string code, string format, params string[] args); | |
/// <summary> | |
/// Log a warning based on the passed in suppression, code, format and additional arguments. | |
/// </summary> | |
/// <param name="suppression">The suppression object which contains the rule information.</param> | |
/// <param name="code">The suppression code</param> | |
/// <param name="format">The message format/param> | |
/// <param name="args">The message format arguments</param> | |
/// <returns>Returns true if the warning is logged and not suppressed.</returns> | |
bool LogWarning(Suppression suppression, string code, string format, params string[] args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) .NET Foundation and contributors. All rights reserved. | |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | |
using Microsoft.NET.Build.Tasks; | |
namespace Microsoft.DotNet.SymbolExtensions.Logging | |
{ | |
/// <summary> | |
/// An MSBuild based log | |
/// </summary> | |
public class MSBuildLog : ILog | |
{ | |
private readonly Logger Log; | |
public MSBuildCompatibilityLogger(Logger log) | |
{ | |
Log = log; | |
} | |
/// <inheritdoc /> | |
public bool LogError(string code, string format, params string[] args) => | |
Log.Log(new Message(MessageLevel.Error, string.Format(format, args), code)); | |
/// <inheritdoc /> | |
public bool LogWarning(Suppression suppression, string code, string format, params string[] args) => | |
Log.Log(new Message(MessageLevel.Warning, string.Format(format, args), code)); | |
/// <inheritdoc /> | |
public void LogMessage(MessageImportance importance, string format, params string[] args) => | |
Log.LogMessage((Build.Framework.MessageImportance)importance, format, args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) .NET Foundation and contributors. All rights reserved. | |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | |
using System; | |
using System.IO; | |
using Microsoft.DotNet.SymbolExtensions.Logging; | |
namespace Microsoft.DotNet.ApiCompat.Tool | |
{ | |
/// <summary> | |
/// A console based log to emit suppressable warnings and errors. | |
/// </summary> | |
internal sealed class SuppressableConsoleLog : ConsoleLog, ISuppressableLog | |
{ | |
private readonly ISuppressionEngine _suppressionEngine; | |
public ConsoleCompatibilityLogger(ISuppressionEngine suppressionEngine, | |
MessageImportance messageImportance) : base(messageImportance) | |
{ | |
_suppressionEngine = suppressionEngine; | |
} | |
/// <inheritdoc /> | |
public bool LogError(Suppression suppression, string code, string format, params string[] args) => | |
LogSuppressableMessage(errorOutput: true, suppression, code, format, args); | |
/// <inheritdoc /> | |
public bool LogWarning(Suppression suppression, string code, string format, params string[] args) => | |
LogSuppressableMessage(errorOutput: false, suppression, code, format, args); | |
private bool LogSuppressableMessage(bool errorOutput, Suppression suppression, string code, string format, params string[] args) | |
{ | |
if (_suppressionEngine.IsErrorSuppressed(suppression)) | |
return false; | |
TextWriter textWriter = errorOutput ? Console.Error : Console.Out; | |
textWriter.WriteLine(code + ": " + string.Format(format, args)); | |
return true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) .NET Foundation and contributors. All rights reserved. | |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | |
using Microsoft.DotNet.SymbolExtensions.Logging; | |
using Microsoft.NET.Build.Tasks; | |
namespace Microsoft.DotNet.ApiCompat.Task | |
{ | |
/// <summary> | |
/// An MSBuild based compatibility logger | |
/// </summary> | |
internal sealed class MSBuildSuppressableLog : MSBuildLog, ISuppressableLog | |
{ | |
private readonly ISuppressionEngine _suppressionEngine; | |
public MSBuildCompatibilityLogger(Logger log, | |
ISuppressionEngine suppressionEngine) : base(log) | |
{ | |
_suppressionEngine = suppressionEngine; | |
} | |
/// <inheritdoc /> | |
public bool LogError(Suppression suppression, string code, string format, params string[] args) => | |
LogSuppressableMessage(MessageLevel.Error, suppression, code, format, args); | |
/// <inheritdoc /> | |
public bool LogWarning(Suppression suppression, string code, string format, params string[] args) => | |
LogSuppressableMessage(MessageLevel.Warning, suppression, code, format, args); | |
private bool LogSuppressableMessage(MessageLevel messageLevel, Suppression suppression, string code, string format, params string[] args) | |
{ | |
if (_suppressionEngine.IsErrorSuppressed(suppression)) | |
return false; | |
Log.Log(new Message(messageLevel, string.Format(format, args), code)); | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment