Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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 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);
        }
        public void name_returns_the_logger_name()
        {
            var service = new DefaultLoggerService();
            var sut     = service.GetLogger("Captain Winters");

            Assert.Equal("Captain Winters", sut.Name);
        }
        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);
        }
Ejemplo n.º 10
0
        public void is_verbose_enabled_returns_correct_value()
        {
            var service = new DefaultLoggerService();
            var sut     = service.GetLogger(this.GetType());

            Assert.True(sut.IsVerboseEnabled);
            service.Threshold = LogLevel.Debug;
            Assert.False(sut.IsVerboseEnabled);
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
        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);
        }