< Summary

Information
Class: ArturRios.Logging.Loggers.ConsoleLogger
Assembly: ArturRios.Logging
File(s): /home/runner/work/dotnet-logging/dotnet-logging/src/Loggers/ConsoleLogger.cs
Line coverage
82%
Covered lines: 32
Uncovered lines: 7
Coverable lines: 39
Total lines: 104
Line coverage: 82%
Branch coverage
27%
Covered branches: 3
Total branches: 11
Branch coverage: 27.2%
Method coverage
100%
Covered methods: 12
Fully covered methods: 11
Total methods: 12
Method coverage: 100%
Full method coverage: 91.6%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.cctor()100%11100%
Trace(...)100%11100%
Debug(...)100%11100%
Info(...)100%11100%
Warn(...)100%11100%
Error(...)100%11100%
Exception(...)100%11100%
Critical(...)100%11100%
Fatal(...)100%11100%
GetAnsiColorSequence(...)11.11%30936.36%
Write(...)100%22100%

File(s)

/home/runner/work/dotnet-logging/dotnet-logging/src/Loggers/ConsoleLogger.cs

#LineLine coverage
 1using ArturRios.Logging.Configuration;
 2using ArturRios.Logging.Factories;
 3using ArturRios.Logging.Interfaces;
 4using ArturRios.Util.Collections;
 5
 6namespace ArturRios.Logging.Loggers;
 7
 8/// <summary>
 9/// Writes log entries to the system console, optionally colored by level.
 10/// </summary>
 11/// <remarks>
 12/// Writes are serialized on a process-wide lock, so entries from concurrent threads never interleave.
 13/// </remarks>
 14/// <param name="configuration">Controls whether ANSI color sequences are emitted.</param>
 2915public class ConsoleLogger(ConsoleLoggerConfiguration configuration) : IInternalLogger
 16{
 117    private static readonly Lock s_writeLock = new();
 18
 19    /// <inheritdoc />
 20    public void Trace(string message, string filePath, string methodName)
 21    {
 322        Write(CustomLogLevel.Trace, filePath, methodName, message);
 323    }
 24
 25    /// <inheritdoc />
 26    public void Debug(string message, string filePath, string methodName)
 27    {
 328        Write(CustomLogLevel.Debug, filePath, methodName, message);
 329    }
 30
 31    /// <inheritdoc />
 32    public void Info(string message, string filePath, string methodName)
 33    {
 2034        Write(CustomLogLevel.Information, filePath, methodName, message);
 2035    }
 36
 37    /// <inheritdoc />
 38    public void Warn(string message, string filePath, string methodName)
 39    {
 240        Write(CustomLogLevel.Warning, filePath, methodName, message);
 241    }
 42
 43    /// <inheritdoc />
 44    public void Error(string message, string filePath, string methodName)
 45    {
 346        Write(CustomLogLevel.Error, filePath, methodName, message);
 347    }
 48
 49    /// <inheritdoc />
 50    public void Exception(string message, string filePath, string methodName)
 51    {
 252        Write(CustomLogLevel.Exception, filePath, methodName, message);
 253    }
 54
 55    /// <inheritdoc />
 56    public void Critical(string message, string filePath, string methodName)
 57    {
 258        Write(CustomLogLevel.Critical, filePath, methodName, message);
 259    }
 60
 61    /// <inheritdoc />
 62    public void Fatal(string message, string filePath, string methodName)
 63    {
 264        Write(CustomLogLevel.Fatal, filePath, methodName, message);
 265    }
 66
 167    private static string GetAnsiColorSequence(CustomLogLevel level) => level switch
 168    {
 069        CustomLogLevel.Trace => AnsiColors.DarkGray,
 070        CustomLogLevel.Debug => AnsiColors.Cyan,
 171        CustomLogLevel.Information => AnsiColors.Green,
 072        CustomLogLevel.Warning => AnsiColors.Yellow,
 073        CustomLogLevel.Error => AnsiColors.Red,
 074        CustomLogLevel.Exception => AnsiColors.Magenta,
 075        CustomLogLevel.Critical or CustomLogLevel.Fatal => AnsiColors.BrightRed,
 076        _ => AnsiColors.White
 177    };
 78
 79    private void Write(CustomLogLevel level, string filePath, string methodName, string message)
 80    {
 3781        var entry = LogEntryFactory.Create(level, filePath, methodName, message);
 82
 3783        if (configuration.UseColors)
 84        {
 185            _ = ConsoleAnsi.EnableVirtualTerminalProcessing();
 86
 187            var ansiColor = GetAnsiColorSequence(level);
 88
 89            const string colorReset = "\e[0m";
 90
 91            lock (s_writeLock)
 92            {
 193                Console.Write(ansiColor + entry + colorReset);
 194            }
 95        }
 96        else
 3697        {
 98            lock (s_writeLock)
 99            {
 36100                Console.Write(entry);
 36101            }
 102        }
 37103    }
 104}