示例#1
0
        private static void ShouldLog(Severity test, Severity should)
        {
            Assert.True(test.ShouldLog(should));
            var msg = MessageEx.Default(should);

            Assert.True(test.ShouldLog(msg));
        }
示例#2
0
        public void TokenStringWorksForNormalMessages()
        {
            var tokenString = WriterEx.TokenString("Test %Severity:U%-%Message%!");

            Assert.Equal("Test DEBUG-MyMessage!", tokenString.ToString(MessageEx.Default(Severity.Debug, "MyMessage")));
            Assert.Equal("Test INFO-%!", tokenString.ToString(MessageEx.Default(Severity.Info, "%")));
        }
示例#3
0
        private static void ShouldNotLog(Severity test, Severity shouldNot)
        {
            Assert.False(test.ShouldLog(shouldNot));
            var msg = MessageEx.Default(shouldNot);

            Assert.False(test.ShouldLog(msg));
        }
示例#4
0
        public void OnlyMessageWorks()
        {
            var tokenString = WriterEx.TokenString("%Message%");

            Assert.Equal("MyMessage", tokenString.ToString(MessageEx.Default(Severity.Debug, "MyMessage")));
            Assert.Equal("%", tokenString.ToString(MessageEx.Default(Severity.Info, "%")));
        }
示例#5
0
        public void PercentWorks()
        {
            var tokenString = WriterEx.TokenString("100%%%Message%");

            Assert.Equal("100%MyMessage", tokenString.ToString(MessageEx.Default(Severity.Debug, "MyMessage")));
            Assert.Equal("100%Ok", tokenString.ToString(MessageEx.Default(Severity.Info, "Ok")));
        }
示例#6
0
        public void FilesOk()
        {
            var tmp = Path.GetTempFileName();

            try
            {
                var writer = new SomeWriter();
                var debug  = MessageEx.Default(Severity.Debug);
                using (var output = OutputEx.File(tmp, writer, false))
                {
                    output.Log(debug);
                    Assert.True(writer.Written.Select(x => x.Item2).SequenceEqual(new[] { debug }));
                    Assert.Equal("1", ReadAllTextTest(tmp));
                }
                using (var output = OutputEx.File(tmp, writer))
                {
                    output.Log(debug);
                    Assert.True(writer.Written.Select(x => x.Item2).SequenceEqual(new[] { debug, debug }));
                    Assert.Equal("12", ReadAllTextTest(tmp));
                }
                using (var output = OutputEx.File(tmp, writer, false))
                {
                    output.Log(debug);
                    Assert.True(writer.Written.Select(x => x.Item2).SequenceEqual(new[] { debug, debug, debug }));
                    Assert.Equal("3", ReadAllTextTest(tmp));
                }
            }
            finally
            {
                File.Delete(tmp);
            }
        }
示例#7
0
        public void LoggerOk()
        {
            var logger  = new SomeLogger("MegaLogger");
            var message = MessageEx.Default(Severity.Fatal, logger: logger);

            Assert.Equal("MegaLogger", message.Get(MessageTokens.Logger));
        }
示例#8
0
        public void NoTokensStringOk()
        {
            const string always      = "Let the force be with you! Always";
            var          tokenString = WriterEx.TokenString(always);

            Assert.Equal(always, tokenString.ToString(MessageEx.Default(Severity.Debug, "MyMessage")));
            Assert.Equal(always, tokenString.ToString(MessageEx.Default(Severity.Info, "%")));
        }
示例#9
0
        public void NowIsNow()
        {
            var now     = DateTime.Now;
            var message = MessageEx.Default(Severity.Fatal);
            var token   = message.Get(MessageTokens.Date, "O");
            var parsed  = DateTime.ParseExact(token, "O", null);

            Assert.InRange(parsed, now, now.AddSeconds(1));
        }
示例#10
0
        public void DefaultOutputsOk()
        {
            var outputs = OutputEx.DefaultOutputs();

            Assert.Equal(0, outputs.Count);
            Assert.True(outputs.SequenceEqual(Enumerable.Empty <IOutput>()));

            var o1      = new SomeOutput();
            var message = MessageEx.Default(Severity.Debug);

            outputs.Add(o1);
            Assert.Equal(1, outputs.Count);
            Assert.True(outputs.SequenceEqual(new[] { o1 }));

            o1.Throw = true;
            outputs.Log(message);
            Assert.Equal(0, o1.Logged.Count);

            o1.Throw = false;
            outputs.Log(message);
            Assert.True(o1.Logged.SequenceEqual(new[] { message }));

            var o2 = new SomeOutput();

            outputs.Add(o2);
            Assert.Equal(2, outputs.Count);
            Assert.True(new[] { o1, o2 }.All(c => outputs.Contains(c)));
            outputs.Log(message);
            Assert.True(o1.Logged.SequenceEqual(new[] { message, message }));
            Assert.True(o2.Logged.SequenceEqual(new[] { message }));
            o1.Throw = true;
            outputs.Log(message);
            Assert.True(o1.Logged.SequenceEqual(new[] { message, message }));
            Assert.True(o2.Logged.SequenceEqual(new[] { message, message }));
            o1.Throw = false;
            o2.Throw = true;
            outputs.Log(message);
            Assert.True(o1.Logged.SequenceEqual(new[] { message, message, message }));
            Assert.True(o2.Logged.SequenceEqual(new[] { message, message }));

            outputs.Remove(o1);
            Assert.Equal(1, outputs.Count);
            Assert.True(outputs.SequenceEqual(new[] { o2 }));

            o2.Throw = false;
            outputs.Log(message);
            Assert.True(o1.Logged.SequenceEqual(new[] { message, message, message }));
            Assert.True(o2.Logged.SequenceEqual(new[] { message, message, message }));

            outputs.Dispose();
            Assert.False(o1.Disposed);
            Assert.True(o2.Disposed);
        }
示例#11
0
 public void TextWriterOk()
 {
     using (var sw = new StringWriter())
     {
         var writer = new SomeWriter();
         var output = OutputEx.TextWriter(sw, writer);
         var msg1   = MessageEx.Default(Severity.Debug);
         var msg2   = MessageEx.Default(Severity.Error);
         output.Log(msg1);
         output.Log(msg2);
         Assert.True(writer.Written.All(x => x.Item1 == sw));
         Assert.True(writer.Written.Select(x => x.Item2).SequenceEqual(new[] { msg1, msg2 }));
         Assert.Equal("12", sw.ToString());
     }
 }
示例#12
0
        public void SeverityOk()
        {
            var message = MessageEx.Default(Severity.Error);

            Assert.Equal("ERROR", message.Get(MessageTokens.Severity, "U"));
            Assert.Equal("ERROR", message.Get(MessageTokens.Severity, "u"));
            Assert.Equal("error", message.Get(MessageTokens.Severity, "L"));
            Assert.Equal("error", message.Get(MessageTokens.Severity, "l"));
            Assert.Equal("Error", message.Get(MessageTokens.Severity));
            message = MessageEx.Default(Severity.Debug);
            Assert.Equal("DEBUG", message.Get(MessageTokens.Severity, "U"));
            Assert.Equal("DEBUG", message.Get(MessageTokens.Severity, "u"));
            Assert.Equal("debug", message.Get(MessageTokens.Severity, "L"));
            Assert.Equal("debug", message.Get(MessageTokens.Severity, "l"));
            Assert.Equal("Debug", message.Get(MessageTokens.Severity));
        }
示例#13
0
        public void OutputSeverityOk()
        {
            var output      = new SomeOutput();
            var errorOutput = output.OutputSeverity(Severity.Error);
            var debugMsg    = MessageEx.Default(Severity.Debug);
            var infoMsg     = MessageEx.Default(Severity.Info);
            var errorMsg    = MessageEx.Default(Severity.Error);
            var fatalMsg    = MessageEx.Default(Severity.Fatal);
            var offMsg      = MessageEx.Default(Severity.Off);

            errorOutput.Log(debugMsg);
            errorOutput.Log(infoMsg);
            errorOutput.Log(errorMsg);
            errorOutput.Log(fatalMsg);
            errorOutput.Log(offMsg);
            Assert.True(output.Logged.SequenceEqual(new[] { errorMsg, fatalMsg }));
        }
示例#14
0
        public void ConsoleOk()
        {
            var writer = new SomeWriter();
            var output = OutputEx.Console(writer);
            var debug  = MessageEx.Default(Severity.Debug);
            var info   = MessageEx.Default(Severity.Info);
            var error  = MessageEx.Default(Severity.Error);
            var fatal  = MessageEx.Default(Severity.Fatal);

            output.Log(debug);
            output.Log(info);
            output.Log(error);
            output.Log(fatal);
            Assert.Equal(Console.Out, writer.Written[0].Item1);
            Assert.Equal(Console.Out, writer.Written[1].Item1);
            Assert.Equal(Console.Error, writer.Written[2].Item1);
            Assert.Equal(Console.Error, writer.Written[3].Item1);
            Assert.True(writer.Written.Select(x => x.Item2).SequenceEqual(new[] { debug, info, error, fatal }));
        }
示例#15
0
        public void MessageOk()
        {
            var message = MessageEx.Default(Severity.Fatal, "TestMessage");

            Assert.Equal("TestMessage", message.Get(MessageTokens.Message));
        }
示例#16
0
 private static IMessage CreateMessage(Severity severity, string msg = null)
 {
     return(MessageEx.Default(severity, msg));
 }