diff --git a/Snowflake.Data.Tests/UnitTests/Logger/EasyLoggerManagerTest.cs b/Snowflake.Data.Tests/UnitTests/Logger/EasyLoggerManagerTest.cs index f9abac8e6..69716492f 100644 --- a/Snowflake.Data.Tests/UnitTests/Logger/EasyLoggerManagerTest.cs +++ b/Snowflake.Data.Tests/UnitTests/Logger/EasyLoggerManagerTest.cs @@ -50,6 +50,38 @@ public void TestThatChangesLogLevel() { // arrange var logger = SFLoggerFactory.GetSFLogger(); + + // act + EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Off, t_directoryLogPath); + + // assert + Assert.IsFalse(logger.IsDebugEnabled()); + Assert.IsFalse(logger.IsInfoEnabled()); + Assert.IsFalse(logger.IsWarnEnabled()); + Assert.IsFalse(logger.IsErrorEnabled()); + Assert.IsFalse(logger.IsFatalEnabled()); + + // act + EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Fatal, t_directoryLogPath); + + // assert + Assert.IsFalse(logger.IsDebugEnabled()); + Assert.IsFalse(logger.IsInfoEnabled()); + Assert.IsFalse(logger.IsWarnEnabled()); + Assert.IsFalse(logger.IsErrorEnabled()); + Assert.IsTrue(logger.IsFatalEnabled()); + + // act + EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Error, t_directoryLogPath); + + // assert + Assert.IsFalse(logger.IsDebugEnabled()); + Assert.IsFalse(logger.IsInfoEnabled()); + Assert.IsFalse(logger.IsWarnEnabled()); + Assert.IsTrue(logger.IsErrorEnabled()); + Assert.IsTrue(logger.IsFatalEnabled()); + + // act EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Warn, t_directoryLogPath); // assert @@ -91,6 +123,48 @@ public void TestThatLogsToProperFileWithProperLogLevelOnly() Assert.That(logLines, Has.Exactly(1).Matches(s => s.Contains(WarnMessage))); Assert.That(logLines, Has.Exactly(1).Matches(s => s.Contains(ErrorMessage))); Assert.That(logLines, Has.Exactly(1).Matches(s => s.Contains(FatalMessage))); + + // arrange + EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Debug, t_directoryLogPath); + + // act + logger.Debug(DebugMessage); + + // assert + logLines = File.ReadLines(FindLogFilePath(t_directoryLogPath, 2)); + Assert.That(logLines, Has.Exactly(1).Matches(s => s.Contains(DebugMessage))); + } + + [Test] + public void TestThatRollsLogIfSizeIsTooBig() + { + // arrange + var logger = SFLoggerFactory.GetSFLogger(); + EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Trace, t_directoryLogPath); + + var appenders = SFLogRepository.s_rootLogger.GetAppenders(); + appenders.Remove(appenders[0]); + var randomFileName = $"snowflake_dotnet_{Path.GetRandomFileName()}"; + var logFileName = randomFileName.Substring(0, randomFileName.Length - 4) + ".log"; + appenders.Add(new SFRollingFileAppender() + { + _name = "RollingFileAppender", + _logFilePath = Path.Combine(t_directoryLogPath, logFileName), + _maximumFileSize = 1, + _maxSizeRollBackups = 2, + _patternLayout = EasyLoggerManager.PatternLayout() + }); + + // act + for (int i = 0; i < 5; i++) + { + logger.Debug(DebugMessage); + System.Threading.Thread.Sleep(1000); + } + var backupLogs = Directory.GetFiles(t_directoryLogPath, $"{logFileName}.*.bak"); + + // assert + Assert.AreEqual(2, backupLogs.Length); } [Test] @@ -131,11 +205,11 @@ private static string RandomLogsDirectoryPath() return Path.Combine(s_logsDirectory, $"easy_logging_logs_{randomName}", "dotnet"); } - private static string FindLogFilePath(string directoryLogPath) + private static string FindLogFilePath(string directoryLogPath, int expectedFileCount = 1) { Assert.IsTrue(Directory.Exists(directoryLogPath)); var files = Directory.GetFiles(directoryLogPath); - Assert.AreEqual(1, files.Length); + Assert.AreEqual(expectedFileCount, files.Length); return files.First(); } diff --git a/Snowflake.Data.Tests/UnitTests/Logger/ILoggerTest.cs b/Snowflake.Data.Tests/UnitTests/Logger/ILoggerTest.cs index c371ac63b..789f80f92 100644 --- a/Snowflake.Data.Tests/UnitTests/Logger/ILoggerTest.cs +++ b/Snowflake.Data.Tests/UnitTests/Logger/ILoggerTest.cs @@ -27,7 +27,7 @@ class ILoggerTest private const string SerilogFileName = "test_serilog.log"; private const string NlogFileName = "test_nlog.log"; - public abstract class SFBaseLoggerTest + public abstract class ILoggerBaseTest { protected ILogger _logger; protected string _logFile; @@ -154,7 +154,7 @@ public void TestThatLogsToProperFileWithProperLogLevelOnly() } [TestFixture] - public class Log4NetTest : SFBaseLoggerTest + public class Log4NetTest : ILoggerBaseTest { [OneTimeSetUp] public void SetUp() @@ -172,7 +172,7 @@ public void SetUp() } [TestFixture] - public class SerilogTest : SFBaseLoggerTest + public class SerilogTest : ILoggerBaseTest { [OneTimeSetUp] public void SetUp() @@ -190,7 +190,7 @@ public void SetUp() } [TestFixture] - public class NlogTest : SFBaseLoggerTest + public class NlogTest : ILoggerBaseTest { [OneTimeSetUp] public void SetUp() diff --git a/Snowflake.Data.Tests/UnitTests/Logger/SFLoggerTest.cs b/Snowflake.Data.Tests/UnitTests/Logger/SFLoggerTest.cs index f7b7cdde2..0c0fc5a71 100644 --- a/Snowflake.Data.Tests/UnitTests/Logger/SFLoggerTest.cs +++ b/Snowflake.Data.Tests/UnitTests/Logger/SFLoggerTest.cs @@ -5,6 +5,7 @@ using NUnit.Framework; using Snowflake.Data.Configuration; using Snowflake.Data.Log; +using System; namespace Snowflake.Data.Tests.UnitTests { @@ -35,7 +36,7 @@ public void AfterTest() } [Test] - public void TestUsingSimpleLogger() + public void TestUsingSFLogger() { SFLoggerFactory.EnableSFLogger(); _logger = SFLoggerFactory.GetSFLogger(); @@ -43,7 +44,7 @@ public void TestUsingSimpleLogger() } [Test] - public void TestSettingCustomLogger() + public void TestUsingEmptyLogger() { SFLoggerFactory.DisableSFLogger(); _logger = SFLoggerFactory.GetSFLogger(); @@ -57,6 +58,7 @@ public void TestIsDebugEnabled( _logger = GetLogger(isEnabled); Assert.AreEqual(isEnabled, _logger.IsDebugEnabled()); + _logger.Debug("debug log message", new Exception("test exception")); } [Test] @@ -66,6 +68,7 @@ public void TestIsInfoEnabled( _logger = GetLogger(isEnabled); Assert.AreEqual(isEnabled, _logger.IsInfoEnabled()); + _logger.Info("info log message", new Exception("test exception")); } [Test] @@ -75,6 +78,7 @@ public void TestIsWarnEnabled( _logger = GetLogger(isEnabled); Assert.AreEqual(isEnabled, _logger.IsWarnEnabled()); + _logger.Warn("warn log message", new Exception("test exception")); } [Test] @@ -84,6 +88,7 @@ public void TestIsErrorEnabled( _logger = GetLogger(isEnabled); Assert.AreEqual(isEnabled, _logger.IsErrorEnabled()); + _logger.Error("error log message", new Exception("test exception")); } [Test] @@ -93,6 +98,75 @@ public void TestIsFatalEnabled( _logger = GetLogger(isEnabled); Assert.AreEqual(isEnabled, _logger.IsFatalEnabled()); + _logger.Fatal("fatal log message", new Exception("test exception")); + } + + [Test] + public void TestGetAppenders( + [Values(false, true)] bool isEnabled) + { + _logger = GetLogger(isEnabled); + if (isEnabled) + { + var appenders = _logger.GetAppenders(); + Assert.IsInstanceOf(appenders[0]); + } + else + { + Assert.Throws(() => _logger.GetAppenders()); + } + } + + [Test] + public void TestAddAppender( + [Values(false, true)] bool isEnabled) + { + _logger = GetLogger(isEnabled); + if (isEnabled) + { + var appenders = _logger.GetAppenders(); + Assert.AreEqual(1, appenders.Count); + _logger.AddAppender(new SFConsoleAppender()); + Assert.AreEqual(2, appenders.Count); + } + else + { + Assert.Throws(() => _logger.AddAppender(new SFConsoleAppender())); + } + } + + [Test] + public void TestRemoveAppender( + [Values(false, true)] bool isEnabled) + { + _logger = GetLogger(isEnabled); + if (isEnabled) + { + var appenders = _logger.GetAppenders(); + Assert.AreEqual(1, appenders.Count); + _logger.RemoveAppender(appenders[0]); + Assert.AreEqual(0, appenders.Count); + } + else + { + Assert.Throws(() => _logger.RemoveAppender(new SFConsoleAppender())); + } + } + + [Test] + public void TestSetLevel( + [Values(false, true)] bool isEnabled) + { + _logger = GetLogger(isEnabled); + if (isEnabled) + { + _logger.SetLevel(LoggingEvent.DEBUG); + Assert.AreEqual(LoggingEvent.DEBUG, ((SFLoggerImpl)_logger)._level); + } + else + { + Assert.Throws(() => _logger.SetLevel(LoggingEvent.DEBUG)); + } } private SFLogger GetLogger(bool isEnabled) @@ -106,7 +180,7 @@ private SFLogger GetLogger(bool isEnabled) SFLoggerFactory.DisableSFLogger(); } - return SFLoggerFactory.GetSFLogger(); + return SFLoggerFactory.GetSFLogger(false); } } } diff --git a/Snowflake.Data/Configuration/EasyLoggingLogLevel.cs b/Snowflake.Data/Configuration/EasyLoggingLogLevel.cs index 450a9bcf1..0b6ebe31c 100644 --- a/Snowflake.Data/Configuration/EasyLoggingLogLevel.cs +++ b/Snowflake.Data/Configuration/EasyLoggingLogLevel.cs @@ -6,9 +6,10 @@ namespace Snowflake.Data.Configuration { - internal enum EasyLoggingLogLevel + public enum EasyLoggingLogLevel { Off, + Fatal, Error, Warn, Info, diff --git a/Snowflake.Data/Logger/EasyLoggingLevelMapper.cs b/Snowflake.Data/Logger/EasyLoggingLevelMapper.cs index 221109237..6364321f2 100644 --- a/Snowflake.Data/Logger/EasyLoggingLevelMapper.cs +++ b/Snowflake.Data/Logger/EasyLoggingLevelMapper.cs @@ -16,6 +16,7 @@ public LoggingEvent ToLoggingEventLevel(EasyLoggingLogLevel level) switch (level) { case EasyLoggingLogLevel.Off: return LoggingEvent.OFF; + case EasyLoggingLogLevel.Fatal: return LoggingEvent.FATAL; case EasyLoggingLogLevel.Error: return LoggingEvent.ERROR; case EasyLoggingLogLevel.Warn: return LoggingEvent.WARN; case EasyLoggingLogLevel.Info: return LoggingEvent.INFO; diff --git a/Snowflake.Data/Logger/SFLoggerFactory.cs b/Snowflake.Data/Logger/SFLoggerFactory.cs index 517d5b1fe..bc695bc3a 100755 --- a/Snowflake.Data/Logger/SFLoggerFactory.cs +++ b/Snowflake.Data/Logger/SFLoggerFactory.cs @@ -45,21 +45,33 @@ public static void SetCustomLogger(ILogger customLogger) SFLoggerFactory.s_customLogger = customLogger; } - internal static SFLogger GetSFLogger() + internal static SFLogger GetSFLogger(bool useFileAppender = true) { // If true, return the default/specified logger if (s_isSFLoggerEnabled) { var logger = new SFLoggerImpl(typeof(T)); - var fileAppender = new SFRollingFileAppender() + if (useFileAppender) { - _name = "RollingFileAppender", - _logFilePath = Path.Combine(Directory.GetCurrentDirectory(), "test_snowflake_log.log"), - _maximumFileSize = 1000000000, // "1GB" - _maxSizeRollBackups = 0, - _patternLayout = EasyLoggerManager.PatternLayout() - }; - logger.AddAppender(fileAppender); + var fileAppender = new SFRollingFileAppender() + { + _name = "RollingFileAppender", + _logFilePath = Path.Combine(Directory.GetCurrentDirectory(), "test_snowflake_log.log"), + _maximumFileSize = 1000000000, // "1GB" + _maxSizeRollBackups = 0, + _patternLayout = EasyLoggerManager.PatternLayout() + }; + logger.AddAppender(fileAppender); + } + else + { + var consoleAppender = new SFConsoleAppender() + { + _name = "ConsoleAppender", + _patternLayout = EasyLoggerManager.PatternLayout() + }; + logger.AddAppender(consoleAppender); + } return logger; } // Else, return the empty logger implementation which outputs nothing diff --git a/Snowflake.Data/Logger/SFLoggerImpl.cs b/Snowflake.Data/Logger/SFLoggerImpl.cs index 03d3d6954..39f924aa3 100644 --- a/Snowflake.Data/Logger/SFLoggerImpl.cs +++ b/Snowflake.Data/Logger/SFLoggerImpl.cs @@ -11,8 +11,6 @@ public static class SFLogRepository { - //internal static Dictionary _logRepository = new Dictionary(); - internal static SFLogger s_rootLogger = s_rootLogger = new SFLoggerImpl(typeof(SFLogRepository)); internal static SFLogger GetRootLogger() @@ -184,8 +182,6 @@ private void SetEnableValues() case LoggingEvent.INFO: _isDebugEnabled = false; break; - default: - break; } } }