Пример #1
0
        public void when_creating_formatter_with_specific_values()
        {
            var formatter = new XmlEventTextFormatter(EventTextFormatting.Indented) { DateTimeFormat = "R" };
            Assert.AreEqual("R", formatter.DateTimeFormat);
            Assert.AreEqual(EventTextFormatting.Indented, formatter.Formatting);


        }
        public void EventWithPayloadKeywrdsNoMsgInXml()
        {
            var logger = MockEventSrcForXml.Logger;
            var formatter = new XmlEventTextFormatter();

            string rawOutput = string.Empty;
            using (var listener = new InMemoryEventListener(formatter))
            {
                listener.EnableEvents(logger, EventLevel.LogAlways, MockEventSrcForXml.Keywords.Errors);
                try
                {
                    logger.UsingKeywords(MockEventSrcForXml.LogMessage, long.MaxValue);
                    rawOutput = listener.ToString();
                }
                finally
                {
                    listener.DisableEvents(logger);
                }
            }

            Assert.AreEqual(-1, rawOutput.IndexOf("\r\n"));
            var entries = XDocument.Parse("<Events>" + rawOutput + "</Events>").Root.Elements();
            XmlFormattedEntry.Fill(entries.Single());
            Assert.AreEqual<Guid>(EventSource.GetGuid(typeof(MockEventSrcForXml)), Guid.Parse(XmlFormattedEntry.Provider.Attribute("Guid").Value));
            Assert.AreEqual<int>(MockEventSrcForXml.UsingKeywordsEventID, Convert.ToInt32(XmlFormattedEntry.EventId.Value));
            Assert.AreEqual<byte>(0, Convert.ToByte(XmlFormattedEntry.Version.Value));
            Assert.AreEqual<int>((int)EventLevel.Informational, Int32.Parse(XmlFormattedEntry.Level.Value));
            Assert.AreEqual<int>((int)MockEventSrcForXml.Tasks.DBQuery, Int32.Parse(XmlFormattedEntry.Task.Value));
            Assert.AreEqual<long>((long)MockEventSrcForXml.Keywords.Errors, Int64.Parse(XmlFormattedEntry.Keywords.Value.Replace("0x", string.Empty)));
            Assert.AreEqual<int>((int)EventOpcode.Start, Int32.Parse(XmlFormattedEntry.Opcode.Value));
            Assert.AreEqual<int>(System.Diagnostics.Process.GetCurrentProcess().Id, Int32.Parse(XmlFormattedEntry.ProcessId.Value));
            Assert.AreEqual<int>(ThreadHelper.GetCurrentUnManagedThreadId(), Int32.Parse(XmlFormattedEntry.ThreadId.Value));
            DateTime dt;
            Assert.IsTrue(DateTime.TryParseExact(XmlFormattedEntry.TimeCreated.Attribute("SystemTime").Value, EventEntry.DefaultDateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt));
            Assert.AreEqual(2, XmlFormattedEntry.Payload.Elements().Count());
            Assert.AreEqual("message", XmlFormattedEntry.Payload.Elements().First().Attribute("Name").Value);
            Assert.AreEqual(MockEventSrcForXml.LogMessage, XmlFormattedEntry.Payload.Elements().First().Value);
            Assert.AreEqual("longArg", XmlFormattedEntry.Payload.Elements().Last().Attribute("Name").Value);
            Assert.AreEqual(long.MaxValue.ToString(), XmlFormattedEntry.Payload.Elements().Last().Value);
        }
        public void WhenPositionalParametersInPayloadInXml()
        {
            var fileNameWithoutExtension = "Task1557_ForRollingFlatFile_XmlFormatter";
            var fileName = fileNameWithoutExtension + ".log";
            FlatFileHelper.DeleteCreatedLogFiles(fileNameWithoutExtension);
            var logger = TestEventSourceNoAttributes.Logger;
            var xmlFormatter = new XmlEventTextFormatter();
            xmlFormatter.DateTimeFormat = "dd/MM/yyyy";

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.LogToRollingFlatFile(fileName, 1, "yyyy", RollFileExistsBehavior.Increment, RollInterval.None, xmlFormatter, 0);
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    for (int i = 0; i < 7; i++)
                    {
                        logger.ObjectArrayEvent4(1000, "stringstringarg10", 2000, "stringstringarg20", 3000);
                    }

                    Assert.IsTrue(File.Exists(fileNameWithoutExtension + "." + DateTime.Now.Year + ".1.log"));
                });

            string readFile = File.ReadAllText(fileName);
            Assert.IsTrue(readFile.Contains("<Message>Check if it is logged</Message>"));
            Assert.IsTrue(readFile.Contains("<Data Name=\"arg0\">1000</Data><Data Name=\"arg1\">stringstringarg10</Data><Data Name=\"arg2\">2000</Data><Data Name=\"arg3\">stringstringarg20</Data><Data Name=\"arg4\">3000</Data>"));
        }
 public void when_creating_formatter_with_default_values()
 {
     var formatter = new XmlEventTextFormatter();
     Assert.IsNull(formatter.DateTimeFormat);
     Assert.AreEqual(EventTextFormatting.None, formatter.Formatting);
 }
 protected override void Given()
 {
     formatter = new XmlEventTextFormatter(EventTextFormatting.Indented);
     listener = new InMemoryEventListener() { Formatter = formatter };
     listener.EnableEvents(logger, EventLevel.LogAlways);
 }
        public void WhenEventWithMessageInAttributeUsingXml()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var xmlFormatter = new XmlEventTextFormatter();
            xmlFormatter.DateTimeFormat = "dd/MM/yyyy";
            var logger = TestEventSourceNoAttributes.Logger;

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.LogToConsole(xmlFormatter);
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    logger.ObjectArrayEvent4(1000, "stringstringarg10", 2000, "stringstringarg20", 3000);
                });

            var entry = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c)).SingleOrDefault();
            Assert.IsNotNull(entry);
            StringAssert.Contains(entry, "<Data Name=\"arg0\">1000</Data><Data Name=\"arg1\">stringstringarg10</Data><Data Name=\"arg2\">2000</Data><Data Name=\"arg3\">stringstringarg20</Data><Data Name=\"arg4\">3000</Data>");
            StringAssert.Contains(entry, "<Message>Check if it is logged</Message>");
        }
        public void WhenPositonalParametersInMessageInXml()
        {
            var fileName = "Task1557.log";
            File.Delete(fileName);
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var xmlformat = new XmlEventTextFormatter();
            xmlformat.DateTimeFormat = "dd/MM/yyyy";
            var logger = TestEventSourceNoAttributes.Logger;

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.LogToFlatFile(fileName, xmlformat);
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    logger.ObjectArrayEvent4(20, "stringarg1", 30, "stringarg3", 40);
                });

            Assert.IsTrue(File.Exists(fileName));
            var entries = Regex.Split(ReadFileWithoutLock(fileName), formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c));
            Assert.AreEqual<int>(1, entries.Count());
            string readFile = File.ReadAllText(fileName);
            Assert.IsTrue(readFile.Contains("<Message>Check if it is logged</Message>"));
            Assert.IsTrue(readFile.Contains("<Data Name=\"arg0\">20</Data><Data Name=\"arg1\">stringarg1</Data><Data Name=\"arg2\">30</Data><Data Name=\"arg3\">stringarg3</Data><Data Name=\"arg4\">40</Data>"));
        }
        public void EventWithActivityIdAndRelatedActivityIdInXml()
        {
            var formatter = new XmlEventTextFormatter();
            var logger = MockEventSrcForXml.Logger;

            var activityId = Guid.NewGuid();
            var relatedActivityId = Guid.NewGuid();
            var previousActivityId = Guid.Empty;
            string rawOutput = string.Empty;
            using (var listener = new InMemoryEventListener(formatter))
            {
                listener.EnableEvents(logger, EventLevel.LogAlways, MockEventSrcForXml.Keywords.Errors);
                try
                {
                    EventSource.SetCurrentThreadActivityId(activityId, out previousActivityId);
                    logger.LogUsingMessageWithRelatedActivityId(MockEventSrcForXml.LogMessage, relatedActivityId);
                    rawOutput = Encoding.Default.GetString(listener.Stream.ToArray());
                }
                finally
                {
                    listener.DisableEvents(logger);
                    EventSource.SetCurrentThreadActivityId(previousActivityId);
                }
            }

            var entries = XDocument.Parse("<Events>" + rawOutput + "</Events>").Root.Elements();
            XmlFormattedEntry.Fill(entries.First());
            Assert.AreEqual<Guid>(EventSource.GetGuid(typeof(MockEventSrcForXml)), Guid.Parse(XmlFormattedEntry.Provider.Attribute("Guid").Value));
            Assert.AreEqual(1, XmlFormattedEntry.Payload.Elements().Count());
            Assert.AreEqual("message", XmlFormattedEntry.Payload.Elements().First().Attribute("Name").Value);
            Assert.AreEqual(MockEventSrcForXml.LogMessage, XmlFormattedEntry.Payload.Elements().First().Value);
            Assert.AreEqual<Guid>(activityId, Guid.Parse(XmlFormattedEntry.Correlation.Attribute("ActivityID").Value));
            Assert.AreEqual<Guid>(relatedActivityId, Guid.Parse(XmlFormattedEntry.Correlation.Attribute("RelatedActivityID").Value));
        }
        public void EventWithPayloadAndEnumsInXml()
        {
            string fileName = "LogUsingPayloadWithEnumsInProc";
            File.Delete(fileName);
            var formatter = new XmlEventTextFormatter(EventTextFormatting.Indented, "d");
            var logger = MockEventSourceInProcEnum.Logger;

            using (var listener = new ObservableEventListener())
            {
                listener.EnableEvents(logger, EventLevel.LogAlways, Keywords.All);
                listener.LogToFlatFile(fileName, formatter);
                try
                {
                    logger.SendEnumsEvent16(MockEventSourceInProcEnum.MyColor.Green, MockEventSourceInProcEnum.MyFlags.Flag1);
                }
                finally
                {
                    listener.DisableEvents(logger);
                }
            }

            var rawOutput = FlatFileHelper.GetAllText(fileName);
            var entries = XDocument.Parse("<Events>" + rawOutput + "</Events>").Root.Elements();
            Assert.AreEqual(1, entries.Count());
            XmlFormattedEntry.Fill(entries.First());
            Assert.AreEqual("a", XmlFormattedEntry.Payload.Elements().First().Attribute("Name").Value);
            Assert.AreEqual("2", XmlFormattedEntry.Payload.Elements().First().Value);
            Assert.AreEqual("b", XmlFormattedEntry.Payload.Elements().Last().Attribute("Name").Value);
            Assert.AreEqual("1", XmlFormattedEntry.Payload.Elements().Last().Value);
        }
        public void EventWithPayloadAloneInXml()
        {
            var formatter = new XmlEventTextFormatter();
            string payloadMsg = ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<test/>");
            var logger = MockEventSrcForXml.Logger;

            string rawOutput = string.Empty;
            using (var listener = new InMemoryEventListener(formatter))
            {
                listener.EnableEvents(logger, EventLevel.LogAlways, MockEventSrcForXml.Keywords.Errors);
                try
                {
                    logger.LogUsingMessage(payloadMsg);
                    rawOutput = Encoding.Default.GetString(listener.Stream.ToArray());
                }
                finally
                {
                    listener.DisableEvents(logger);
                }
            }

            var entries = XDocument.Parse("<Events>" + rawOutput + "</Events>").Root.Elements();
            XmlFormattedEntry.Fill(entries.First());
            Assert.IsFalse(rawOutput.StartsWith("{\r\n")); // No Formatting (Default)
            Assert.AreEqual<Guid>(EventSource.GetGuid(typeof(MockEventSrcForXml)), Guid.Parse(XmlFormattedEntry.Provider.Attribute("Guid").Value));
            Assert.AreEqual<int>(MockEventSrcForXml.LogUsingMessageEventID, Convert.ToInt32(XmlFormattedEntry.EventId.Value));
            Assert.AreEqual<byte>(0, Convert.ToByte(XmlFormattedEntry.Version.Value));
            Assert.AreEqual<int>((int)EventLevel.Informational, Int32.Parse(XmlFormattedEntry.Level.Value));
            Assert.AreEqual<int>((int)MockEventSrcForXml.Tasks.Page, Int32.Parse(XmlFormattedEntry.Task.Value));
            Assert.AreEqual<long>((long)EventKeywords.None, Int64.Parse(XmlFormattedEntry.Keywords.Value.Replace("0x", string.Empty)));
            Assert.AreEqual<int>((int)EventOpcode.Start, Int32.Parse(XmlFormattedEntry.Opcode.Value));
            Assert.AreEqual<byte>(0, Convert.ToByte(XmlFormattedEntry.Version.Value));
            Assert.AreEqual<int>(System.Diagnostics.Process.GetCurrentProcess().Id, Int32.Parse(XmlFormattedEntry.ProcessId.Value));
            Assert.AreEqual<int>(ThreadHelper.GetCurrentUnManagedThreadId(), Int32.Parse(XmlFormattedEntry.ThreadId.Value));
            DateTime dt;
            Assert.IsTrue(DateTime.TryParseExact(XmlFormattedEntry.TimeCreated.Attribute("SystemTime").Value, EventEntry.DefaultDateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt));
            Assert.AreEqual(1, XmlFormattedEntry.Payload.Elements().Count());
            Assert.AreEqual("message", XmlFormattedEntry.Payload.Elements().First().Attribute("Name").Value);
            Assert.AreEqual(payloadMsg, XmlFormattedEntry.Payload.Elements().First().Value);
        }
        public void TwoEventsWithPayloadsAndMessageInXml()
        {
            var formatter = new XmlEventTextFormatter();
            var logger = MockEventSrcForXml.Logger;

            string rawOutput = string.Empty;
            using (var listener = new InMemoryEventListener(formatter))
            {
                listener.EnableEvents(logger, EventLevel.LogAlways, MockEventSrcForXml.Keywords.Errors);
                try
                {
                    logger.LogUsingMessage(MockEventSrcForXml.LogMessage);
                    logger.LogUsingMessage(MockEventSrcForXml.LogMessage + "2");
                    rawOutput = Encoding.Default.GetString(listener.Stream.ToArray());
                }
                finally
                {
                    listener.DisableEvents(logger);
                }
            }

            var entries = XDocument.Parse("<Events>" + rawOutput + "</Events>").Root.Elements();
            Assert.AreEqual(2, entries.Count());
            XmlFormattedEntry.Fill(entries.First());
            Assert.AreEqual("message", XmlFormattedEntry.Payload.Elements().First().Attribute("Name").Value);
            Assert.AreEqual(MockEventSrcForXml.LogMessage, XmlFormattedEntry.Payload.Elements().First().Value);
            XmlFormattedEntry.Fill(entries.Last());
            Assert.AreEqual("message", XmlFormattedEntry.Payload.Elements().Last().Attribute("Name").Value);
            Assert.AreEqual(MockEventSrcForXml.LogMessage + "2", XmlFormattedEntry.Payload.Elements().First().Value);
            Assert.AreEqual(MockEventSrcForXml.LogMessage, XmlFormattedEntry.Message.Elements().First().Value);
        }