< Summary

Information
Class: GridForge.GridForgeLogger
Assembly: GridForge
File(s): /home/runner/work/GridForge/GridForge/src/GridForge/Utility/GridForgeLogger.cs
Line coverage
90%
Covered lines: 37
Uncovered lines: 4
Coverable lines: 41
Total lines: 147
Line coverage: 90.2%
Branch coverage
100%
Covered branches: 7
Total branches: 7
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
get_Channel()100%11100%
get_DebugChannel()100%11100%
get_EnableDebugLogging()100%11100%
set_EnableDebugLogging(...)100%11100%
get_LogHandler()100%11100%
set_LogHandler(...)100%11100%
get_CustomFormatter()100%11100%
set_CustomFormatter(...)100%11100%
get_LogFilePath()100%11100%
set_LogFilePath(...)100%11100%
get_MinimumLevel()100%11100%
set_MinimumLevel(...)100%11100%
IsEnabled(...)100%210%
DefaultLogHandler(...)100%210%
DefaultLogFormatter(...)100%210%
.ctor()100%11100%
get_LogFilePath()100%11100%
DefaultLogHandler(...)100%22100%
DefaultLogFormatter(...)100%55100%

File(s)

/home/runner/work/GridForge/GridForge/src/GridForge/Utility/GridForgeLogger.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3
 4namespace GridForge;
 5
 6/// <summary>
 7/// Provides a configurable logging system for GridForge with support for log levels, formatting, and file output.
 8/// </summary>
 9public static class GridForgeLogger
 10{
 111    private static readonly GridForgeDiagnosticLogger _logger = new();
 12
 13    /// <summary>
 14    /// Gets the diagnostic channel used by GridForge logging.
 15    /// </summary>
 68416    public static DiagnosticChannel Channel => _logger.Channel;
 17
 18    /// <summary>
 19    /// Gets the diagnostic channel used for verbose debug diagnostics.
 20    /// </summary>
 321    public static DiagnosticChannel DebugChannel => _logger.DebugChannel;
 22
 23    /// <summary>
 24    /// Gets or sets a value indicating whether verbose debug diagnostics should be emitted.
 25    /// </summary>
 26    public static bool EnableDebugLogging
 27    {
 328        get => _logger.EnableDebugLogging;
 629        set => _logger.EnableDebugLogging = value;
 30    }
 31
 32    /// <summary>
 33    /// Gets or sets the delegate used to write formatted log messages.
 34    /// Assigning <see langword="null"/> restores <see cref="DefaultLogHandler"/>.
 35    /// </summary>
 36    public static Action<DiagnosticLevel, string, string> LogHandler
 37    {
 1438        get => _logger.LogHandler;
 2739        set => _logger.LogHandler = value;
 40    }
 41
 42    /// <summary>
 43    /// Gets or sets the formatter used to transform log arguments into a final log entry.
 44    /// Assigning <see langword="null"/> restores <see cref="DefaultLogFormatter"/>.
 45    /// </summary>
 46    public static Func<DiagnosticLevel, string, string, string> CustomFormatter
 47    {
 848        get => _logger.CustomFormatter;
 949        set => _logger.CustomFormatter = value;
 50    }
 51
 52    /// <summary>
 53    /// Gets or sets the file path for logging. If null, file logging is disabled.
 54    /// </summary>
 55    public static string? LogFilePath
 56    {
 257        get => _logger.LogFilePath;
 458        set => _logger.LogFilePath = value;
 59    }
 60
 61    /// <summary>
 62    /// Gets or sets the minimum log level required for messages to be logged.
 63    /// </summary>
 64    public static DiagnosticLevel MinimumLevel
 65    {
 1466        get => _logger.MinimumLevel;
 2967        set => _logger.MinimumLevel = value;
 68    }
 69
 70    /// <summary>
 71    /// Determines whether diagnostics at the specified level are currently enabled.
 72    /// </summary>
 73    /// <param name="level">The diagnostic level to evaluate.</param>
 74    /// <returns><see langword="true"/> when messages at <paramref name="level"/> will be emitted; otherwise, <see langw
 75    public static bool IsEnabled(DiagnosticLevel level)
 76    {
 077        return _logger.IsEnabled(level);
 78    }
 79
 80    /// <summary>
 81    /// The default handler for logging messages, writing them to the console and optionally to a file.
 82    /// </summary>
 83    /// <param name="level">The severity level of the log message.</param>
 84    /// <param name="message">The log message.</param>
 85    /// <param name="source">The source of the log message.</param>
 86    public static void DefaultLogHandler(DiagnosticLevel level, string message, string source)
 87    {
 088        _logger.DefaultLogHandler(level, message, source);
 089    }
 90
 91    /// <summary>
 92    /// The default log formatter that formats log messages with timestamp, log level, and source information.
 93    /// </summary>
 94    /// <param name="level">The severity level of the log message.</param>
 95    /// <param name="message">The log message.</param>
 96    /// <param name="source">The source of the log message.</param>
 97    /// <returns>A formatted log entry as a string.</returns>
 98    public static string DefaultLogFormatter(DiagnosticLevel level, string message, string source)
 99    {
 0100        return _logger.DefaultLogFormatter(level, message, source);
 101    }
 102
 103    private sealed class GridForgeDiagnosticLogger : DiagnosticLogger
 104    {
 1105        private readonly object _lock = new();
 106
 2107        public GridForgeDiagnosticLogger() : base("GridForge") { }
 108
 28109        public string? LogFilePath { get; set; }
 110
 111        public override void DefaultLogHandler(DiagnosticLevel level, string message, string source)
 112        {
 20113            lock (_lock)
 114            {
 20115                string logEntry = CustomFormatter(level, message, source);
 20116                Console.WriteLine(logEntry);
 117
 20118                if (!string.IsNullOrEmpty(LogFilePath))
 119                {
 120                    try
 121                    {
 2122                        File.AppendAllText(LogFilePath, logEntry + Environment.NewLine);
 1123                    }
 1124                    catch (Exception ex)
 125                    {
 1126                        Console.WriteLine($"[ERROR] Failed to write to log file: {ex.Message}");
 1127                    }
 128                }
 18129            }
 20130        }
 131
 132        public override string DefaultLogFormatter(DiagnosticLevel level, string message, string source)
 133        {
 23134            var timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff");
 23135            var levelTag = level switch
 23136            {
 1137                DiagnosticLevel.None => "[NONE]",
 2138                DiagnosticLevel.Info => "[INFO]",
 1139                DiagnosticLevel.Warning => "[WARN]",
 18140                DiagnosticLevel.Error => "[ERROR]",
 1141                _ => "[LOG]"
 23142            };
 143
 23144            return $"{timestamp} {levelTag} [{source}] {message}";
 145        }
 146    }
 147}