Exemplo n.º 1
0
        public void NamedArguments()
        {
            LogManager.Start();
            MemoryLogger memoryLog = LogManager.CreateMemoryLogger();

            memoryLog.SubscribeToEvents(TestLogger.Write, EventLevel.Verbose);
            memoryLog.FormatOptions &= ~TextLogFormatOptions.ProcessAndThreadData;
            TestLogger.Write.Element(0xbe, 0xef, "cake", 2.1, true);
            memoryLog.Disabled = true;

            memoryLog.Stream.Seek(0, SeekOrigin.Begin);
            using (var reader = new StreamReader(memoryLog.Stream))
            {
                string line;
                int    lines = 0;
                while ((line = reader.ReadLine()) != null)
                {
                    ++lines;
                    Assert.IsTrue(
                        line.Contains(
                            "[i:TestLogger OnlyTask/Extension] wind=190 water=239 earth=\"cake\" fire=2.1 heart=True"));
                }
                Assert.AreEqual(1, lines);
            }
            memoryLog.Dispose();
            LogManager.Shutdown();
        }
Exemplo n.º 2
0
        public void DisabledFlag()
        {
            LogManager.Start();
            MemoryLogger memoryLog = LogManager.CreateMemoryLogger();

            memoryLog.SubscribeToEvents(TestLogger.Write, EventLevel.Verbose);
            TestLogger.Write.String("bacon");
            TestLogger.Write.String("lettuce");
            memoryLog.Disabled = true;
            TestLogger.Write.String("avacado");
            memoryLog.Disabled = false;
            TestLogger.Write.String("tomato");
            memoryLog.Disabled = true;
            memoryLog.Stream.Seek(0, SeekOrigin.Begin);

            // Shouldn't be any avacado in there. S'weird.
            using (var reader = new StreamReader(memoryLog.Stream))
            {
                int    lines = 0;
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    ++lines;
                    Assert.IsFalse(line.Contains("avacado"));
                }
                Assert.AreEqual(3, lines);
            }
            memoryLog.Dispose();
            LogManager.Shutdown();
        }
Exemplo n.º 3
0
        public void BasicTextWriting()
        {
            LogManager.Start();
            MemoryLogger memoryLog = LogManager.CreateMemoryLogger();

            memoryLog.SubscribeToEvents(TestLogger.Write, EventLevel.Verbose);
            TestLogger.Write.String("first message");
            TestLogger.Write.String("second\nmessage");
            // we used to test passing \0 characters here -- EventSource in .NET 4.5.1 correctly treats these as string
            // terminators, however in 4.5 it does not. We need to operate in both worlds for now, so we'll ignore this
            // for the time being.
            memoryLog.Disabled = true;

            memoryLog.Stream.Seek(0, SeekOrigin.Begin);
            using (var reader = new StreamReader(memoryLog.Stream))
            {
                string line;
                int    lines = 0;
                while ((line = reader.ReadLine()) != null)
                {
                    ++lines;
                    var expectedMetaData = string.Format("[{0}/{1}/v:TestLogger String]", Process.GetCurrentProcess().Id,
                                                         NativeMethods.GetCurrentWin32ThreadId());
                    Assert.IsTrue(line.Contains(expectedMetaData));
                    // this is the pretty name of the event we should have deduced.
                    switch (lines)
                    {
                    case 1:
                        Assert.IsTrue(line.EndsWith(" message=\"first message\""));
                        break;

                    case 2:
                        Assert.IsTrue(line.EndsWith(" message=\"second\\nmessage\""));
                        break;
                    }
                }
                Assert.AreEqual(2, lines);
            }
            memoryLog.Dispose();
            LogManager.Shutdown();
        }
 public void Dispose()
 {
     _log.Provider.WriteLogs(_console.WriteLine);
     _log?.Dispose();
 }
Exemplo n.º 5
0
        public void Timestamps()
        {
            LogManager.Start();
            MemoryLogger memoryLog = LogManager.CreateMemoryLogger();

            memoryLog.SubscribeToEvents(TestLogger.Write, EventLevel.Verbose);
            memoryLog.FormatOptions &= ~TextLogFormatOptions.ProcessAndThreadData;
            // showing activity ID should be the default
            Assert.IsTrue(memoryLog.FormatOptions.HasFlag(TextLogFormatOptions.Timestamp));
            TestLogger.Write.String("normal timestamp");
            memoryLog.FormatOptions &= ~TextLogFormatOptions.Timestamp;
            memoryLog.FormatOptions |= TextLogFormatOptions.TimeOffset;
            Thread.Sleep(200); // wait a bit to ensure we get a decent offset time
            TestLogger.Write.String("with offset");
            memoryLog.FormatOptions &= ~TextLogFormatOptions.TimeOffset;
            TestLogger.Write.String("no time");
            memoryLog.Disabled = true;

            memoryLog.Stream.Seek(0, SeekOrigin.Begin);
            using (var reader = new StreamReader(memoryLog.Stream))
            {
                string line;
                int    lines = 0;
                while ((line = reader.ReadLine()) != null)
                {
                    ++lines;
                    switch (lines)
                    {
                    case 1:
                    {
                        string[] split = line.Split(new[] { ' ' }, 2);
                        Assert.AreEqual(2, split.Length);
                        DateTime.ParseExact(split[0], EventStringFormatter.TimeFormat, CultureInfo.InvariantCulture);
                        Assert.IsTrue(0 ==
                                      string.Compare(split[1], "[v:TestLogger String] message=\"normal timestamp\"",
                                                     StringComparison.Ordinal));
                        break;
                    }

                    case 2:
                    {
                        string[] split = line.Split(new[] { ' ' }, 2);
                        Assert.AreEqual(2, split.Length);
                        float offset;
                        Assert.IsTrue(float.TryParse(split[0], NumberStyles.AllowDecimalPoint,
                                                     CultureInfo.InvariantCulture, out offset));
                        Assert.IsTrue(offset > 0 && offset < 100); // shouldn't take 100 seconds to test this :)
                        Assert.IsTrue(0 == string.Compare(split[1], "[v:TestLogger String] message=\"with offset\"",
                                                          StringComparison.Ordinal));
                        break;
                    }

                    case 3:
                    {
                        Assert.IsTrue(0 == string.Compare(line, "[v:TestLogger String] message=\"no time\"",
                                                          StringComparison.Ordinal));
                        break;
                    }
                    }
                }
                Assert.AreEqual(3, lines);
            }
            memoryLog.Dispose();
            LogManager.Shutdown();
        }
Exemplo n.º 6
0
        public void Filtering()
        {
            LogManager.Start();
            MemoryLogger memoryLog = LogManager.CreateMemoryLogger();

            memoryLog.SubscribeToEvents(TestLogger.Write, EventLevel.Verbose);
            TestLogger.Write.String("first message");
            TestLogger.Write.String("bacon...bacon...bacon... OMG BACON");
            memoryLog.Disabled = true;
            memoryLog.Stream.Seek(0, SeekOrigin.Begin);

            // Nothing filtered.
            using (var reader = new StreamReader(memoryLog.Stream))
            {
                int lines = 0;
                while ((reader.ReadLine()) != null)
                {
                    ++lines;
                }
                Assert.AreEqual(2, lines);
            }
            memoryLog.Dispose();

            memoryLog = LogManager.CreateMemoryLogger();
            memoryLog.SubscribeToEvents(TestLogger.Write, EventLevel.Verbose);
            memoryLog.AddRegexFilter("first");
            TestLogger.Write.String("first message");
            TestLogger.Write.String("bacon...bacon...bacon... OMG BACON");
            memoryLog.Disabled = true;
            memoryLog.Stream.Seek(0, SeekOrigin.Begin);

            // Should now get just the one message
            using (var reader = new StreamReader(memoryLog.Stream))
            {
                int    lines = 0;
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    Assert.IsFalse(line.Contains("bacon"));
                    ++lines;
                }
                Assert.AreEqual(1, lines);
            }
            memoryLog.Dispose();

            memoryLog = LogManager.CreateMemoryLogger();
            memoryLog.SubscribeToEvents(TestLogger.Write, EventLevel.Verbose);
            memoryLog.AddRegexFilter("BaCoN"); // filter for bacon, ensure REs are not case sensitive.
            TestLogger.Write.String("first message");
            TestLogger.Write.String("bacon...bacon...bacon... OMG BACON");
            memoryLog.Disabled = true;
            memoryLog.Stream.Seek(0, SeekOrigin.Begin);

            // Now we should get the second only.
            using (var reader = new StreamReader(memoryLog.Stream))
            {
                int    lines = 0;
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    ++lines;
                    Assert.IsTrue(line.Contains("bacon"));
                }
                Assert.AreEqual(1, lines);
            }
            memoryLog.Dispose();
            LogManager.Shutdown();
        }
Exemplo n.º 7
0
        public void ActivityIDFormat()
        {
            const string activityID = "d00dfeedbeeffeedbeefd00dfeedbeef"; // I implore thee!

            LogManager.Start();
            MemoryLogger memoryLog = LogManager.CreateMemoryLogger();

            memoryLog.FormatOptions &= ~TextLogFormatOptions.ProcessAndThreadData;

            memoryLog.SubscribeToEvents(TestLogger.Write, EventLevel.Verbose);
            // showing activity ID should be the default
            Assert.IsTrue(memoryLog.FormatOptions.HasFlag(TextLogFormatOptions.ShowActivityID));
            LogManager.ClearActivityId();
            TestLogger.Write.String("ID is empty");
            LogManager.SetActivityId(new Guid(activityID));
            TestLogger.Write.String("has ID");
            memoryLog.FormatOptions &= ~TextLogFormatOptions.ShowActivityID;
            TestLogger.Write.String("no ID");
            LogManager.ClearActivityId();
            memoryLog.Disabled = true;

            memoryLog.Stream.Seek(0, SeekOrigin.Begin);
            using (var reader = new StreamReader(memoryLog.Stream))
            {
                string line;
                int    lines = 0;
                while ((line = reader.ReadLine()) != null)
                {
                    ++lines;
                    switch (lines)
                    {
                    case 1:
                    {
                        string[] split = line.Split(new[] { ' ' }, 2);
                        Assert.AreEqual(2, split.Length); // first split should be time, then the message
                        DateTime.ParseExact(split[0], EventStringFormatter.TimeFormat, CultureInfo.InvariantCulture);
                        Assert.IsTrue(0 == string.Compare(split[1], "[v:TestLogger String] message=\"ID is empty\"",
                                                          StringComparison.Ordinal));
                        break;
                    }

                    case 2:
                    {
                        string[] split = line.Split(new[] { ' ' }, 3);
                        Assert.AreEqual(3, split.Length);
                        DateTime.ParseExact(split[0], EventStringFormatter.TimeFormat, CultureInfo.InvariantCulture);
                        Assert.AreEqual(34, split[1].Length);
                        Assert.AreEqual('(', split[1][0]);
                        Assert.AreEqual(')', split[1][33]);
                        Assert.IsTrue(new Guid(split[1].Substring(1, 32)) == new Guid(activityID));
                        Assert.IsTrue(0 == string.Compare(split[2], "[v:TestLogger String] message=\"has ID\"",
                                                          StringComparison.Ordinal));
                        break;
                    }

                    case 3:
                    {
                        string[] split = line.Split(new[] { ' ' }, 2);
                        Assert.AreEqual(2, split.Length);
                        DateTime.ParseExact(split[0], EventStringFormatter.TimeFormat, CultureInfo.InvariantCulture);
                        Assert.IsTrue(0 == string.Compare(split[1], "[v:TestLogger String] message=\"no ID\"",
                                                          StringComparison.Ordinal));
                        break;
                    }
                    }
                }
                Assert.AreEqual(3, lines);
            }
            memoryLog.Dispose();
            LogManager.Shutdown();
        }
Exemplo n.º 8
0
        public void ActivityIDFilter()
        {
            Guid goodID = Guid.NewGuid();
            Guid skipID = Guid.NewGuid();

            Assert.AreNotEqual(goodID, skipID); // oh paranoia how I love.. loathe.. like thee.

            LogManager.Start();
            MemoryLogger memoryLog = LogManager.CreateMemoryLogger();

            memoryLog.SubscribeToEvents(TestLogger.Write, EventLevel.Verbose);
            memoryLog.FormatOptions = TextLogFormatOptions.ShowActivityID; // no timestamps please
            Assert.AreEqual(memoryLog.FilterActivityID, Guid.Empty);
            LogManager.ClearActivityId();
            TestLogger.Write.String("not yet");
            memoryLog.FilterActivityID = goodID;
            TestLogger.Write.String("now filtered");
            LogManager.SetActivityId(goodID);
            TestLogger.Write.String("good ID");
            LogManager.ClearActivityId();
            TestLogger.Write.String("no ID");
            LogManager.SetActivityId(skipID);
            TestLogger.Write.String("skipped ID");
            memoryLog.FilterActivityID = Guid.Empty;
            TestLogger.Write.String("not anymore");
            LogManager.ClearActivityId();
            memoryLog.Disabled = true;

            memoryLog.Stream.Seek(0, SeekOrigin.Begin);
            using (var reader = new StreamReader(memoryLog.Stream))
            {
                string line;
                int    lines = 0;
                while ((line = reader.ReadLine()) != null)
                {
                    ++lines;
                    switch (lines)
                    {
                    case 1:
                    {
                        Assert.IsTrue(0 == string.Compare(line, "[v:TestLogger String] message=\"not yet\"",
                                                          StringComparison.Ordinal));
                        break;
                    }

                    case 2:
                    {
                        string[] split = line.Split(new[] { ' ' }, 2);
                        Assert.AreEqual(2, split.Length);
                        Assert.AreEqual(34, split[0].Length);
                        Assert.AreEqual('(', split[0][0]);
                        Assert.AreEqual(')', split[0][33]);
                        Assert.IsTrue(new Guid(split[0].Substring(1, 32)) == goodID);
                        Assert.IsTrue(0 == string.Compare(split[1], "[v:TestLogger String] message=\"good ID\"",
                                                          StringComparison.Ordinal));
                        break;
                    }

                    case 3:
                    {
                        string[] split = line.Split(new[] { ' ' }, 2);
                        Assert.AreEqual(2, split.Length);
                        Assert.AreEqual(34, split[0].Length);
                        Assert.AreEqual('(', split[0][0]);
                        Assert.AreEqual(')', split[0][33]);
                        Assert.IsTrue(new Guid(split[0].Substring(1, 32)) == skipID);
                        Assert.IsTrue(0 == string.Compare(split[1], "[v:TestLogger String] message=\"not anymore\"",
                                                          StringComparison.Ordinal));
                        break;
                    }
                    }
                }
                Assert.AreEqual(3, lines);
            }
            memoryLog.Dispose();
            LogManager.Shutdown();
        }