예제 #1
0
        public void ShouldSuccessfullyInvokeSpecificLogService(bool isDatabase, bool isFile, bool isConsole)
        {
            var logLevelParms = new LogLevelParameters { Message = true, Warning = true, Error = true };

            _databaseServiceMock.Setup(s => s.Log(logLevelParms, ValidMessage));

            _fileServiceMock.Setup(s => s.Log(logLevelParms, ValidMessage));

            _consoleServiceMock.Setup(s => s.Log(logLevelParms, ValidMessage));

            var targetParameters = new LogTargetParameters
            {
                DataBase = isDatabase,
                File = isFile,
                Console = isConsole
            };

            _sut.Configure(targetParameters, new LogLevelParameters { Message = true });

            _sut.LogMessage(ValidMessage, logLevelParms);

            _databaseServiceMock.Verify(s => s.Log(logLevelParms, ValidMessage), isDatabase ? Times.Once() : Times.Never());

            _fileServiceMock.Verify(s => s.Log(logLevelParms, ValidMessage), isFile ? Times.Once() : Times.Never());

            _consoleServiceMock.Verify(s => s.Log(logLevelParms, ValidMessage), isConsole ? Times.Once() : Times.Never());
        }
예제 #2
0
        public void Log(LogLevelParameters level, string message)
        {
            var connection = new SqlConnection(ConnectionString, null);
            connection.Open();

            var levelCode = 0;
            if (LevelSettings.Message && level.Message)
            {
                levelCode = 1;
            }
            if (LevelSettings.Error && level.Error)
            {
                levelCode = 2;
            }
            if (level.Warning && level.Warning)
            {
                levelCode = 3;
            }

            var command = new SqlCommand("INSERT INTO Log VALUES('@message', @levelCode)");
            command.Parameters.Add(new SqlParameter("@message", message));
            command.Parameters.Add(new SqlParameter("@levelCode", levelCode));
            command.ExecuteNonQuery();
            connection.Close();
        }
예제 #3
0
 public void Configure(LogTargetParameters targetParameters, LogLevelParameters levelParameters)
 {
     _targetSettings = targetParameters;
     _levelSettings = levelParameters;
     _databaseService.LevelSettings = _levelSettings;
     _fileService.LevelSettings = _levelSettings;
     _consoleService.LevelSettings = _levelSettings;
 }
예제 #4
0
        public void Log(LogLevelParameters level, string message)
        {
            var line = string.Empty;

            var logFileName = Path.Combine(LogFileDirectory, string.Format("LogFile{0:d}.txt", DateTime.Now));

            if (!File.Exists(logFileName))
            {
                line = File.ReadAllText(logFileName);
            }

            File.WriteAllText(logFileName, string.Format("{0}{1:d} {2}", line, DateTime.Now, message));
        }
예제 #5
0
        public void ShouldThrowExceptionWithAllFalseLevelSettings()
        {
            var logLevelParms = new LogLevelParameters { Message = true, Warning = true, Error = true };

            _sut.Configure(new LogTargetParameters{ Console = true }, new LogLevelParameters());

            var actual = Assert.Throws<Exception>(() =>
            {
                _sut.LogMessage(ValidMessage, logLevelParms);
            });

            Assert.AreSame(MyLogger.InvalidLevelExceptionMessage, actual.Message);
        }
예제 #6
0
        public void Log(LogLevelParameters levelParameters, string message)
        {
            if (LevelSettings.Error && levelParameters.Error)
            {
                Console.ForegroundColor = ConsoleColor.Red;
            }
            if (LevelSettings.Warning && levelParameters.Warning)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
            }
            if (LevelSettings.Message && levelParameters.Message)
            {
                Console.ForegroundColor = ConsoleColor.White;
            }

            Console.WriteLine("{0:d} {1}", DateTime.Now, message);
        }
예제 #7
0
        private static void ExecuteMyLogger()
        {
            Console.ReadKey();

            try
            {
                var target = new LogTargetParameters {Console = true};
                var level = new LogLevelParameters { Error = true, Message = true, Warning = true };
                MyLogger.Instance.Configure(target, level);
                MyLogger.Instance.LogMessage(" Logger.init  ", new LogLevelParameters { Error = true, Message = true });
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error:" + ex.Message);
            }

            Console.ReadKey();
        }
예제 #8
0
        public void LogMessage(string message, LogLevelParameters levelParameters)
        {
            if (string.IsNullOrWhiteSpace(message))
            {
                return;
            }

            message = message.Trim();

            if (_targetSettings == null
                || (_targetSettings != null && !_targetSettings.Console && !_targetSettings.File && !_targetSettings.DataBase))
            {
                throw new Exception(InvalidConfigurationExceptionMessage);
            }

            if (_levelSettings == null
                || (_levelSettings != null && !_levelSettings.Error && !_levelSettings.Message && !_levelSettings.Warning)
                || (levelParameters != null && !levelParameters.Message && !levelParameters.Warning && !levelParameters.Error))
            {
                throw new Exception(InvalidLevelExceptionMessage);
            }

            if (_targetSettings.DataBase)
            {
                _databaseService.Log(levelParameters, message);
            }

            if (_targetSettings.File)
            {
                _fileService.Log(levelParameters, message);
            }

            if (_targetSettings.Console)
            {
                _consoleService.Log(levelParameters, message);
            }
        }
예제 #9
0
        public void ShouldThrowExceptionWithNullSettings()
        {
            var logLevelParms = new LogLevelParameters { Message = true, Warning = true, Error = true };

            var actual = Assert.Throws<Exception>(() =>
            {
                _sut.LogMessage(ValidMessage, logLevelParms);
            });

            Assert.AreSame(MyLogger.InvalidConfigurationExceptionMessage, actual.Message);
        }