< Summary

Information
Class: SwitchBlade.Core.Logger
Assembly: SwitchBlade
File(s): D:\a\switchblade\switchblade\Core\Logger.cs
Tag: 203_23722840422
Line coverage
100%
Covered lines: 45
Uncovered lines: 0
Coverable lines: 45
Total lines: 89
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_IsDebugEnabled()100%11100%
.cctor()100%11100%
get_Instance()100%11100%
get_LogFilePath()100%11100%
get_LogPath()100%11100%
SwitchBlade.Contracts.ILogger.get_IsDebugEnabled()100%11100%
SwitchBlade.Contracts.ILogger.set_IsDebugEnabled(...)100%11100%
SwitchBlade.Contracts.ILogger.Log(...)100%11100%
SwitchBlade.Contracts.ILogger.LogError(...)100%11100%
LogStatic(...)100%22100%
Log(...)100%11100%
LogErrorStatic(...)100%11100%
LogError(...)100%11100%
LogErrorStatic(...)100%11100%
LogError(...)100%11100%

File(s)

D:\a\switchblade\switchblade\Core\Logger.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3
 4namespace SwitchBlade.Core
 5{
 6    public class Logger : SwitchBlade.Contracts.ILogger
 7    {
 1668        public static bool IsDebugEnabled { get; set; } = false;
 19        private static readonly object _lock = new object();
 10
 11        // Singleton instance for static bridge
 712        public static Logger Instance { get; } = new Logger();
 13
 14        // Allow tests to override the log path
 3515        public static string LogFilePath { get; set; } = Path.Combine(Path.GetTempPath(), "switchblade_debug.log");
 16
 917        private static string LogPath => LogFilePath;
 18
 19        // Instance methods implementing ILogger
 20        bool SwitchBlade.Contracts.ILogger.IsDebugEnabled
 21        {
 222            get => IsDebugEnabled;
 223            set => IsDebugEnabled = value;
 24        }
 25
 726        void SwitchBlade.Contracts.ILogger.Log(string message) => LogStatic(message);
 127        void SwitchBlade.Contracts.ILogger.LogError(string context, Exception ex) => LogErrorStatic(context, ex);
 28
 29        // Static methods for backward compatibility
 30        public static void LogStatic(string message)
 5931        {
 11532            if (!IsDebugEnabled) return;
 33
 34            try
 335            {
 336                var line = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] {message}{Environment.NewLine}";
 337                lock (_lock)
 338                {
 339                    File.AppendAllText(LogPath, line);
 240                }
 241            }
 142            catch (Exception)
 143            {
 44                // Silently fail to avoid recursive crashes
 145            }
 5946        }
 47
 48        // Bridge for old static calls
 5249        public static void Log(string message) => LogStatic(message);
 50
 51
 52        public static void LogErrorStatic(string message)
 253        {
 54            try
 255            {
 256                var line = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] ERROR: {message}{Environment.NewLine}";
 257                lock (_lock)
 258                {
 259                    File.AppendAllText(LogPath, line);
 160                }
 161            }
 362            catch { }
 263        }
 64
 65        // Bridge for old static calls
 266        public static void LogError(string message) => LogErrorStatic(message);
 67
 68
 69        public static void LogErrorStatic(string context, Exception ex)
 470        {
 71            // We force logging for errors even if debug is disabled,
 72            // but we might want to reconsider if strict silence is required.
 73            // For now, let's allow errors to be logged to the new temp location for diagnostics.
 74            try
 475            {
 476                var line = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] ERROR [{context}]: {ex.Message}\nStack: {ex.StackT
 477                lock (_lock)
 478                {
 479                    File.AppendAllText(LogPath, line);
 380                }
 381            }
 382            catch { }
 483        }
 84
 85        // Bridge for old static calls
 386        public static void LogError(string context, Exception ex) => LogErrorStatic(context, ex);
 87    }
 88
 89}