Example #1
0
 public void EndMessageTest_DisposedObject()
 {
     Assert.Throws <ObjectDisposedException>(() =>
     {
         LiveLogXMLWriter livewriter = new LiveLogXMLWriter(new MemoryStream(), testSettings, 1000);
         livewriter.Dispose();
         livewriter.EndWriteMessage();
     });
 }
Example #2
0
        public void BeginEndMessageTest_DoubleBegin()
        {
            Assert.Throws <InvalidOperationException>(() =>
            {
                LiveLogXMLWriter livewriter = new LiveLogXMLWriter(new MemoryStream(), testSettings, 1000);

                livewriter.BeginWriteMessage(true);
                livewriter.BeginWriteMessage(true);
            });
        }
Example #3
0
        public void BeginEndMessageTest_OverwriteNonExistingLastMessage()
        {
            MemoryStream     output     = new MemoryStream();
            LiveLogXMLWriter livewriter = new LiveLogXMLWriter(output, testSettings, 1000);
            XmlWriter        writer;

            writer = livewriter.BeginWriteMessage(true);
            writer.WriteStartElement("test3");
            writer.WriteEndElement();
            livewriter.EndWriteMessage();

            TestStreamContent(output, "<test3 />");
        }
Example #4
0
 protected override void LiveLogListen(CancellationToken stopEvt, LiveLogXMLWriter output)
 {
     using (host.Trace.NewFrame)
     {
         try
         {
             if (azureConnectParams.Mode == AzureConnectionParams.LoadMode.FixedRange)
             {
                 ReportBackgroundActivityStatus(true);
                 foreach (var entry in AzureDiagnosticsUtils.LoadEntriesRange(
                              table, new EntryPartition(azureConnectParams.From.Ticks), new EntryPartition(azureConnectParams.Till.Ticks), null, stopEvt))
                 {
                     WriteEntry(entry.Entry, output);
                     if (stopEvt.IsCancellationRequested)
                     {
                         return;
                     }
                 }
                 ReportBackgroundActivityStatus(false);
                 return;
             }
             else if (azureConnectParams.Mode == AzureConnectionParams.LoadMode.Recent)
             {
                 ReportBackgroundActivityStatus(true);
                 var lastPartition = AzureDiagnosticsUtils.FindLastMessagePartitionKey(table, DateTime.UtcNow, stopEvt);
                 if (lastPartition.HasValue)
                 {
                     var firstPartition = new EntryPartition(lastPartition.Value.Ticks + azureConnectParams.Period.Ticks);
                     foreach (var entry in AzureDiagnosticsUtils.LoadEntriesRange(table, firstPartition, EntryPartition.MaxValue, null, stopEvt))
                     {
                         WriteEntry(entry.Entry, output);
                         stopEvt.ThrowIfCancellationRequested();
                     }
                 }
                 ReportBackgroundActivityStatus(false);
                 return;
             }
         }
         catch (OperationCanceledException e)
         {
             host.Trace.Error(e, "WAD live log thread cancelled");
         }
         catch (Exception e)
         {
             host.Trace.Error(e, "WAD live log thread failed");
         }
     }
 }
Example #5
0
 protected override void LiveLogListen(CancellationToken stopEvt, LiveLogXMLWriter output)
 {
     using (this.trace.NewFrame)
     {
         try
         {
             var query = CreateQuery();
             for (EventBookmark lastReadBookmark = null; ;)
             {
                 ReportBackgroundActivityStatus(true);
                 using (var reader = new EventLogReader(query, lastReadBookmark))
                 {
                     for (; ;)
                     {
                         using (var eventInstance = reader.ReadEvent())
                         {
                             if (eventInstance == null)
                             {
                                 break;
                             }
                             if (stopEvt.IsCancellationRequested)
                             {
                                 return;
                             }
                             WriteEvent(eventInstance, output);
                             lastReadBookmark = eventInstance.Bookmark;
                         }
                     }
                 }
                 ReportBackgroundActivityStatus(false);
                 if (eventLogIdentity.Type == EventLogIdentity.EventLogType.File)
                 {
                     break;
                 }
                 if (stopEvt.IsCancellationRequested)
                 {
                     return;
                 }
             }
         }
         catch (Exception e)
         {
             this.trace.Error(e, "EVT live log thread failed");
         }
     }
 }
Example #6
0
        void WriteEvent(EventRecord eventRecord, LiveLogXMLWriter output)
        {
            XmlWriter writer = output.BeginWriteMessage(false);

            writer.WriteStartElement("m");
            writer.WriteAttributeString("d", Listener.FormatDate(eventRecord.TimeCreated.GetValueOrDefault()));
            writer.WriteAttributeString("t", GetEventThreadId(eventRecord));
            var s = GetEventSeverity(eventRecord);

            if (s != null)
            {
                writer.WriteAttributeString("s", s);
            }
            writer.WriteString(GetEventDescription(eventRecord));
            writer.WriteEndElement();
            output.EndWriteMessage();
        }
Example #7
0
        protected override Task LiveLogListen(CancellationToken stopEvt, LiveLogXMLWriter output)
        {
            using (this.trace.NewFrame)
            {
                try
                {
                    bufferReadyEvt.Set();
                    long         msgIdx = 1;
                    WaitHandle[] evts   = new WaitHandle[] { dataReadyEvt, stopEvt.WaitHandle };

                    while (true)
                    {
                        int evtIdx = WaitHandle.WaitAny(evts);
                        if (evtIdx == 1)
                        {
                            break;
                        }

                        IntPtr addr    = bufferAddress.DangerousGetHandle();
                        UInt32 appID   = (UInt32)Marshal.ReadInt32(addr);
                        long   strAddr = addr.ToInt64() + sizeof(UInt32);
                        string msg     = string.Format("{0} [{1}] {2}",
                                                       msgIdx, appID, Marshal.PtrToStringAnsi(new IntPtr(strAddr)));

                        XmlWriter writer = output.BeginWriteMessage(false);
                        writer.WriteStartElement("m");
                        writer.WriteAttributeString("d", Listener.FormatDate(DateTime.Now));
                        writer.WriteAttributeString("t", "Process " + appID.ToString());
                        writer.WriteString(msg);
                        writer.WriteEndElement();
                        output.EndWriteMessage();

                        ++msgIdx;

                        bufferReadyEvt.Set();
                    }
                }
                catch (Exception e)
                {
                    this.trace.Error(e, "DebugOutput listening thread failed");
                }
            }
            return(Task.CompletedTask);
        }
Example #8
0
        void WriteEntry(AzureDiagnosticLogEntry entry, LiveLogXMLWriter output)
        {
            XmlWriter writer = output.BeginWriteMessage(false);

            writer.WriteStartElement("m");
            writer.WriteAttributeString("d", Listener.FormatDate(new DateTime(entry.EventTickCount, DateTimeKind.Utc).ToLocalTime()));
            writer.WriteAttributeString("t", string.Format("{0}-{1}", entry.Pid, entry.Tid));
            if (entry.Level <= 2)
            {
                writer.WriteAttributeString("s", "e");
            }
            else if (entry.Level == 3)
            {
                writer.WriteAttributeString("s", "w");
            }
            writer.WriteString(strategy.GetEntryMessage(entry));
            writer.WriteEndElement();
            output.EndWriteMessage();
        }
Example #9
0
        public void LimitSizeTest()
        {
            string testStr = "1234567890qwertyuiopasdfghjklzxcvbnm";

            for (int maxSizeInBytes = 4; maxSizeInBytes < testStr.Length * 2; maxSizeInBytes += 4)
            {
                MemoryStream output = new MemoryStream();

                using (LiveLogXMLWriter writer = new LiveLogXMLWriter(output, testSettings, maxSizeInBytes))
                {
                    writer.BeginWriteMessage(false).WriteString(testStr);
                    writer.EndWriteMessage();

                    Assert.IsTrue(output.Length < maxSizeInBytes);
                    string actual = LiveLogXMLWriter.OutputEncoding.GetString(output.ToArray());
                    Assert.IsTrue(actual.Length > 0);
                    Assert.IsTrue(testStr.EndsWith(actual));
                }
            }
        }
Example #10
0
        void DoCloseOutputTest(bool close)
        {
            XmlWriterSettings tmp = testSettings.Clone();

            tmp.CloseOutput = close;

            MyStream output = new MyStream();

            using (LiveLogXMLWriter livewriter = new LiveLogXMLWriter(output, tmp, 1000))
            {
                livewriter.BeginWriteMessage(false).WriteString("aaa");
                livewriter.EndWriteMessage();
                livewriter.BeginWriteMessage(false).WriteString("bbb");
                livewriter.EndWriteMessage();
                livewriter.BeginWriteMessage(true).WriteString("ccc");
                livewriter.EndWriteMessage();
            }

            Assert.AreEqual(close, output.IsDisposed);
        }
Example #11
0
        public void BeginEndMessageTest_Normal()
        {
            MemoryStream     output     = new MemoryStream();
            LiveLogXMLWriter livewriter = new LiveLogXMLWriter(output, testSettings, 1000);
            XmlWriter        writer;

            writer = livewriter.BeginWriteMessage(false);
            writer.WriteStartElement("test");
            writer.WriteAttributeString("attr", "val");
            writer.WriteFullEndElement();
            livewriter.EndWriteMessage();

            TestStreamContent(output, "<test attr=\"val\"></test>");

            writer = livewriter.BeginWriteMessage(false);
            writer.WriteStartElement("test2");
            writer.WriteAttributeString("attr2", "val");
            writer.WriteFullEndElement();
            livewriter.EndWriteMessage();

            TestStreamContent(output, "<test attr=\"val\"></test><test2 attr2=\"val\"></test2>");
        }
Example #12
0
        protected override async Task LiveLogListen(CancellationToken stopEvt, LiveLogXMLWriter output)
        {
            using (ILogMedia media = await SimpleFileMedia.Create(
                       host.FileSystem,
                       SimpleFileMedia.CreateConnectionParamsFromFileName(fileName)))
                using (FileSystemWatcher watcher = new FileSystemWatcher(Path.GetDirectoryName(fileName),
                                                                         Path.GetFileName(fileName)))
                    using (AutoResetEvent fileChangedEvt = new AutoResetEvent(true))
                    {
                        IMessagesSplitter splitter = new MessagesSplitter(
                            new StreamTextAccess(media.DataStream, Encoding.ASCII, TextStreamPositioningParams.Default),
                            host.RegexFactory.Create(@"^(?<body>.+)$", ReOptions.Multiline)
                            );

                        watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size;
                        watcher.Changed     += delegate(object sender, FileSystemEventArgs e)
                        {
                            fileChangedEvt.Set();
                        };
                        //watcher.EnableRaisingEvents = true;

                        long         lastLinePosition = 0;
                        long         lastStreamLength = 0;
                        WaitHandle[] events           = new WaitHandle[] { stopEvt.WaitHandle, fileChangedEvt };

                        var capture = new TextMessageCapture();

                        for (; ;)
                        {
                            if (WaitHandle.WaitAny(events, 250, false) == 0)
                            {
                                break;
                            }

                            await media.Update();

                            if (media.Size == lastStreamLength)
                            {
                                continue;
                            }

                            lastStreamLength = media.Size;
                            sizeInBytesStat  = lastStreamLength;

                            DateTime lastModified = media.LastModified;

                            await splitter.BeginSplittingSession(new FileRange.Range(0, lastStreamLength), lastLinePosition, MessagesParserDirection.Forward);

                            try
                            {
                                for (; ;)
                                {
                                    if (!await splitter.GetCurrentMessageAndMoveToNextOne(capture))
                                    {
                                        break;
                                    }
                                    lastLinePosition = capture.BeginPosition;

                                    XmlWriter writer = output.BeginWriteMessage(false);
                                    writer.WriteStartElement("m");
                                    writer.WriteAttributeString("d", Listener.FormatDate(lastModified));
                                    writer.WriteString(XmlUtils.RemoveInvalidXMLChars(capture.MessageHeader));
                                    writer.WriteEndElement();
                                    output.EndWriteMessage();
                                }
                            }
                            finally
                            {
                                splitter.EndSplittingSession();
                            }
                        }
                    }
        }