Exemplo n.º 1
0
        public void TestLogLevels2()
        {
            //Arrange (use the XML AppSettings to init the logging framework
            var header              = "TestLogLevels2";
            var logFilePath         = $"{LogFolder}/Some.LogFile2.log";
            var log4netConfigFile   = "myLog4net.config";
            var runOnSeparateThread = false;                                                                                    //to avoid delay in logging

            File.WriteAllText(log4netConfigFile, ExistingLog4NetConfigText.Replace(log4netConfiguredLogFilePath, logFilePath)); //will be restored in test cleanup

            if (File.Exists(LoggingManager.Logging_ConfigFileName))                                                             //delete dedicated json config file, if any
            {
                File.Delete(LoggingManager.Logging_ConfigFileName);
            }
            if (File.Exists(settingsFile)) //delete the JSON appsettings file as well, to force fallback on XML app.config
            {
                File.Delete(settingsFile);
            }

            var msg     = "Some-456 Text";
            var pattern = $".*{msg.Replace(" ", "\\s")}";


            //Act
            var logMgr = new LoggingManager(header, log4netConfigFile, runOnSeparateThread);

            Console.WriteLine($"LoggingManager hash {logMgr.GetHashCode()}");
            ExecuteAssertions(header, runOnSeparateThread, log4netConfigFile, logFilePath); //as specified in app.config of this project

            var logger = logMgr.GetLogger <MefConfigTests>();

            logger.Info($"log file: {logFilePath}");
            foreach (var lev in LogLevels)
            {
                logger.Log(msg, level: lev);
            }

            //Assert
            ValidateLogEntry(logFilePath, pattern);

            if (File.Exists(log4netConfigFile))
            {
                File.Delete(log4netConfigFile);
            }
        }
Exemplo n.º 2
0
        public void TestLogLevels3()
        {
            //Arrange (use the XML AppSettings to init the logging framework
            var header              = "TestLogLevels3";
            var logFilePath         = $"{LogFolder}/Some.LogFile3.log";
            var log4netConfigFile   = "xyzLog4net.config";
            var runOnSeparateThread = false;                                                                                    //to avoid delay in logging

            File.WriteAllText(log4netConfigFile, ExistingLog4NetConfigText.Replace(log4netConfiguredLogFilePath, logFilePath)); //will be restored in test cleanup
            SetupJsonConfigFile(header, log4netConfigFile, runOnSeparateThread);

            if (File.Exists(settingsFile)) //delete the JSON appsettings file as well, to force fallback on XML app.config
            {
                File.Delete(settingsFile);
            }

            string pattern(string txt) => $".*{txt.Replace(" ", "\\s")}";

            //Act - 1
            var logMgr = Mef.Resolve <ILogManager>();

            //Assert - 1
            ExecuteAssertions(header, runOnSeparateThread, log4netConfigFile, logFilePath); //as specified in app.config of this project

            //Act - 2
            var logger = logMgr.GetLogger <MefConfigTests>();

            logger.Info($"log file: {logFilePath}");

            //Assert = 2
            ValidateLogEntry(logFilePath, pattern(logFilePath), new[] { LogLevel.Info });

            //Act - 3
            logger = logMgr.GetLogger(typeof(GeneralConfigTests));
            logger.Warn("abc");

            //Assert - 3
            ValidateLogEntry(logFilePath, pattern("abc"), new[] { LogLevel.Warn });

            if (File.Exists(log4netConfigFile))
            {
                File.Delete(log4netConfigFile);
            }
        }
Exemplo n.º 3
0
        public void InitLoggingUsingMefAndJsonConfig()
        {
            //Arrange
            var header              = "ABC";
            var log4netConfigPath   = "log4netTmp.config"; //we will create this file as a copy of the existing log4net.config file, which we delete and retore at the end
            var logFilePath         = $"{LogFolder}/Tmp.log";
            var runOnSeparateThread = false;

            File.WriteAllText(log4netConfigPath, ExistingLog4NetConfigText.Replace(log4netConfiguredLogFilePath, logFilePath));
            SetupJsonConfigFile(header, log4netConfigPath, runOnSeparateThread);

            //Act
            var logger = Mef.Resolve <ILogManager>();

            //Assert
            ExecuteAssertions(logger, header, runOnSeparateThread, log4netConfigPath, logFilePath);

            //Cleanup for this test only
            DeleteLog4netConfig(log4netConfigPath);
        }
Exemplo n.º 4
0
        public void TestLogLevels()
        {
            //Arrange - use custom JSON config file for logging
            var header              = "TestLogLevels";
            var logFilePath         = $"{LogFolder}/Some.LogFile.log";
            var log4netConfigFile   = "myOtherLog4net.config";
            var runOnSeparateThread = false;                                                                                    //to avoid delay in logging

            File.WriteAllText(log4netConfigFile, ExistingLog4NetConfigText.Replace(log4netConfiguredLogFilePath, logFilePath)); //will be restored in test cleanup
            SetupJsonConfigFile(header, log4netConfigFile, runOnSeparateThread);

            if (File.Exists(settingsFile)) //delete the JSON appsettings file as well, to force fallback on XML app.config
            {
                File.Delete(settingsFile);
            }

            var ex  = new Exception("Test");
            var msg = "Some-123 Message";
            var pat = $".*{msg.Replace(" ", "\\s")}";

            //Act
            var logMgr = Mef.Resolve <ILogManager>();

            Console.WriteLine($"LoggingManager hash {logMgr.GetHashCode()}");
            ExecuteAssertions(logMgr, header, runOnSeparateThread, log4netConfigFile, logFilePath); //as specified in app.config of this project

            var logger = logMgr.GetLogger <LoggingTests>();

            foreach (var lev in LogLevels)
            {
                logger.Log(msg, ex, lev);
            }

            //Assert
            ValidateLogEntry(logFilePath, pat);

            if (File.Exists(log4netConfigFile))
            {
                File.Delete(log4netConfigFile);
            }
        }
Exemplo n.º 5
0
        public void InitLoggingUsingParamCtorAndIgnoringAllOtherConfig()
        {
            //Arrange
            (string header, string cfgHeader) = ("Hello Logger", "Not Used Header");
            (string log4netConfigPath, string cfgLog4netConfigPath) = ("helloLog4net.config", "uselessLog4Net.config");
            (string logFilePath, string cfgLogFilePath)             = ($"{LogFolder}/HelloLog.log", $"{LogFolder}/NoLog.log");
            (bool runOnSeparateThread, bool cfgRunOnSeparateThread) = (false, true);
            File.WriteAllText(log4netConfigPath, ExistingLog4NetConfigText.Replace(log4netConfiguredLogFilePath, logFilePath));       //setting up the used log4net config file
            File.WriteAllText(cfgLog4netConfigPath, ExistingLog4NetConfigText.Replace(log4netConfiguredLogFilePath, cfgLogFilePath)); //setting up the configured (not used) log4net config file

            SetupAppSettingsJsonConfigFile(cfgHeader, cfgLog4netConfigPath, cfgRunOnSeparateThread);                                  //setting up the not-used config file
            SetupJsonConfigFile(header, log4netConfigPath, runOnSeparateThread);                                                      //setting up the not-used config file

            //Act
            var logger = new LoggingManager(header, log4netConfigPath, runOnSeparateThread); //input params will override all configured params

            //Assert
            ExecuteAssertions(logger, header, runOnSeparateThread, log4netConfigPath, logFilePath);

            //Cleanup for this test only
            DeleteLog4netConfig(log4netConfigPath);
        }
Exemplo n.º 6
0
        public void InitLoggingUsingDefaultCtorAndXmlAppSettingsConfig()
        {
            //Arrange
            var logFilePath = $"{LogFolder}/Some.LogFile.log";

            File.WriteAllText(Log4netConfig, ExistingLog4NetConfigText.Replace(log4netConfiguredLogFilePath, logFilePath)); //will be restored in test cleanup

            if (File.Exists(LoggingManager.Logging_ConfigFileName))                                                         //delete dedicated json config file, if any
            {
                File.Delete(LoggingManager.Logging_ConfigFileName);
            }
            if (File.Exists(settingsFile)) //delete the JSON appsettings file as well, to force fallback on XML app.config
            {
                File.Delete(settingsFile);
            }

            //Act
            var logger = new LoggingManager();

            //Assert
            ExecuteAssertions(logger, "*** START Unit tests (Samples) ***", true, Log4netConfig, logFilePath); //as specified in app.config of this project
        }
Exemplo n.º 7
0
        public void InitLoggingUsingDefaultCtorAndAppSettingsJsonConfig()
        {
            //Arrange
            var header              = "*****START*****";
            var log4netConfigPath   = "myLog4net.config"; //we will create this file as a copy of the existing log4net.config file, which we delete and retore at the end
            var logFilePath         = $"{LogFolder}/MyLog.log";
            var runOnSeparateThread = false;

            File.WriteAllText(log4netConfigPath, ExistingLog4NetConfigText.Replace(log4netConfiguredLogFilePath, logFilePath));
            SetupAppSettingsJsonConfigFile(header, log4netConfigPath, runOnSeparateThread);
            if (File.Exists(LoggingManager.Logging_ConfigFileName)) //delete dedicated json config file, if any
            {
                File.Delete(LoggingManager.Logging_ConfigFileName);
            }

            //Act
            var logger = new LoggingManager();

            //Assert
            ExecuteAssertions(logger, header, runOnSeparateThread, log4netConfigPath, logFilePath);

            //Cleanup for this test only
            DeleteLog4netConfig(log4netConfigPath);
        }