示例#1
0
        public void get_logger_for_type_returns_a_logger_with_the_full_name_of_the_type_passed_in()
        {
            var sut    = new DefaultLoggerService();
            var logger = sut.GetLogger(this.GetType());

            Assert.Equal(this.GetType().FullName, logger.Name);
        }
        public void name_returns_the_logger_name()
        {
            var service = new DefaultLoggerService();
            var sut     = service.GetLogger("Captain Winters");

            Assert.Equal("Captain Winters", sut.Name);
        }
示例#3
0
        public void get_logger_by_name_returns_a_logger_with_the_specified_name()
        {
            var sut    = new DefaultLoggerService();
            var logger = sut.GetLogger("Some Logger");

            Assert.Equal("Some Logger", logger.Name);
        }
示例#4
0
        public void threshold_determines_which_log_entries_tick()
        {
            var sut     = new DefaultLoggerService();
            var logger  = sut.GetLogger(this.GetType());
            var entries = new List <LogEntry>();

            sut.Entries.Subscribe(entries.Add);

            logger.Debug("Expected 1");
            logger.Info("Expected 2");
            sut.Threshold = LogLevel.Info;
            logger.Debug("NOT expected");
            logger.Info("Expected 3");
            logger.Warn("Expected 4");
            sut.Threshold = LogLevel.Perf;
            logger.Info("NOT expected");
            logger.Perf("Expected 5").Dispose();
            sut.Threshold = LogLevel.Warn;
            logger.Perf("NOT expected").Dispose();
            logger.Warn("Expected 6");
            sut.Threshold = LogLevel.Error;
            logger.Warn("NOT expected");
            logger.Error("Expected 7");
            sut.Threshold = LogLevel.None;
            logger.Error("NOT expected");

            Assert.Equal(7, entries.Count);
            Assert.Equal("Expected 1", entries[0].Message);
            Assert.Equal("Expected 2", entries[1].Message);
            Assert.Equal("Expected 3", entries[2].Message);
            Assert.Equal("Expected 4", entries[3].Message);
            Assert.StartsWith("Expected 5 [", entries[4].Message);
            Assert.Equal("Expected 6", entries[5].Message);
            Assert.Equal("Expected 7", entries[6].Message);
        }
        public void logging_formatted_message_with_exception_generates_appropriate_log_entry(LogLevel level, string format, object[] args)
        {
            var service  = new DefaultLoggerService();
            var sut      = service.GetLogger(this.GetType());
            var logEntry = default(LogEntry);

            service.Entries.Subscribe(x => logEntry = x);

            var exception  = new Exception("Circular logic detected.");
            var parameters = new object[] { sut, exception, format }
            .Concat(args)
            .ToArray();
            var typeParameters = args
                                 .Select(arg => arg.GetType())
                                 .ToArray();

            typeof(LoggerExtensions)
            .GetTypeInfo()
            .DeclaredMethods
            .Where(
                method =>
                method.Name == level.ToString() &&
                method.GetParameters().Length == parameters.Length &&
                method.GetParameters()[1].ParameterType == typeof(Exception) &&
                method.GetParameters()[3].ParameterType != typeof(object[]))
            .Single()
            .MakeGenericMethod(typeParameters)
            .Invoke(null, parameters);

            Assert.Equal(this.GetType().FullName, logEntry.Name);
            Assert.Equal(level, logEntry.Level);
            Assert.Equal(Environment.CurrentManagedThreadId, logEntry.ThreadId);
            Assert.StartsWith(string.Format(format, args) + "System.Exception: Circular logic detected.", logEntry.Message);
        }
示例#6
0
        public void entries_ticks_for_every_log_entry()
        {
            var sut     = new DefaultLoggerService();
            var entries = new List <LogEntry>();

            sut.Entries.Subscribe(entries.Add);
            var logger1 = sut.GetLogger("First");
            var logger2 = sut.GetLogger("Second");

            logger1.Debug("Debug message.");
            logger1.Warn("Some warning.");
            logger2.Info("Something from logger 2.");
            logger1.Debug("Another debug message.");
            logger2.Error("Something bad happened.");

            Assert.Equal(5, entries.Count);
            Assert.Equal(LogLevel.Debug, entries[0].Level);
            Assert.Equal("Debug message.", entries[0].Message);
            Assert.Equal(LogLevel.Warn, entries[1].Level);
            Assert.Equal("Some warning.", entries[1].Message);
            Assert.Equal(LogLevel.Info, entries[2].Level);
            Assert.Equal("Something from logger 2.", entries[2].Message);
            Assert.Equal(LogLevel.Debug, entries[3].Level);
            Assert.Equal("Another debug message.", entries[3].Message);
            Assert.Equal(LogLevel.Error, entries[4].Level);
            Assert.Equal("Something bad happened.", entries[4].Message);
        }
示例#7
0
        public void is_error_enabled_always_returns_true()
        {
            var sut = new DefaultLoggerService();

            Assert.True(sut.IsErrorEnabled);
            sut.Threshold = LogLevel.Error;
            Assert.True(sut.IsErrorEnabled);
        }
示例#8
0
        public void is_warn_enabled_returns_correct_value()
        {
            var sut = new DefaultLoggerService();

            Assert.True(sut.IsWarnEnabled);
            sut.Threshold = LogLevel.Error;
            Assert.False(sut.IsWarnEnabled);
        }
示例#9
0
        public void is_perf_enabled_returns_correct_value()
        {
            var sut = new DefaultLoggerService();

            Assert.True(sut.IsPerfEnabled);
            sut.Threshold = LogLevel.Warn;
            Assert.False(sut.IsPerfEnabled);
        }
示例#10
0
        public void is_verbose_enabled_returns_correct_value()
        {
            var sut = new DefaultLoggerService();

            Assert.True(sut.IsVerboseEnabled);
            sut.Threshold = LogLevel.Debug;
            Assert.False(sut.IsVerboseEnabled);
        }
示例#11
0
        public void get_logger_for_type_returns_the_same_logger_for_the_same_type()
        {
            var sut     = new DefaultLoggerService();
            var logger1 = sut.GetLogger(this.GetType());
            var logger2 = sut.GetLogger(this.GetType());

            Assert.Same(logger1, logger2);
        }
示例#12
0
        public void is_debug_enabled_returns_correct_value()
        {
            var sut = new DefaultLoggerService();

            Assert.True(sut.IsDebugEnabled);
            sut.Threshold = LogLevel.Info;
            Assert.False(sut.IsDebugEnabled);
        }
示例#13
0
        public void get_logger_by_name_returns_the_same_logger_for_the_same_name()
        {
            var sut     = new DefaultLoggerService();
            var logger1 = sut.GetLogger("Name");
            var logger2 = sut.GetLogger("Name");

            Assert.Same(logger1, logger2);
        }
示例#14
0
        public void threshold_can_be_set()
        {
            var sut = new DefaultLoggerService();

            sut.Threshold = LogLevel.Error;
            Assert.Equal(LogLevel.Error, sut.Threshold);
            sut.Threshold = LogLevel.Info;
            Assert.Equal(LogLevel.Info, sut.Threshold);
        }
        public void is_error_enabled_returns_correct_value()
        {
            var service = new DefaultLoggerService();
            var sut     = service.GetLogger(this.GetType());

            Assert.True(sut.IsErrorEnabled);
            service.Threshold = LogLevel.None;
            Assert.False(sut.IsErrorEnabled);
        }
        public void perf_logs_only_once_disposable_is_disposed()
        {
            var service = new DefaultLoggerService();
            var sut     = service.GetLogger(this.GetType());
            var logged  = false;

            service.Entries.Subscribe(x => logged = true);

            var disposable = sut.Perf("Here is the message.");

            Assert.False(logged);
            disposable.Dispose();
            Assert.True(logged);
        }
        public void logging_message_generates_appropriate_log_entry(LogLevel level, string message)
        {
            var service  = new DefaultLoggerService();
            var sut      = service.GetLogger(this.GetType());
            var logEntry = default(LogEntry);

            service.Entries.Subscribe(x => logEntry = x);

            var result = typeof(LoggerExtensions)
                         .GetTypeInfo()
                         .DeclaredMethods
                         .Where(method => method.Name == level.ToString() && method.GetParameters().Length == 2)
                         .Single()
                         .Invoke(null, new object[] { sut, message });

            (result as IDisposable)?.Dispose();

            Assert.Equal(this.GetType().FullName, logEntry.Name);
            Assert.Equal(level, logEntry.Level);
            Assert.Equal(Environment.CurrentManagedThreadId, logEntry.ThreadId);
            Assert.StartsWith(message, logEntry.Message);
        }
示例#18
0
        public void threshold_can_be_retrieved()
        {
            var sut = new DefaultLoggerService();

            Assert.Equal(LogLevel.Debug, sut.Threshold);
        }
示例#19
0
        public void entries_is_not_null()
        {
            var sut = new DefaultLoggerService();

            Assert.NotNull(sut.Entries);
        }