Exemplo n.º 1
0
        public void MultipleTrackEventLogFileWithManagementTest()
        {
            IWorkContext context    = WorkContext.Empty;
            string       tempFolder = Path.Combine(Path.GetTempPath(), "TelemetryTest");

            Directory.CreateDirectory(tempFolder);
            string logFileName;

            using (EventRouter router = new EventRouter(context))
            {
                IEventDataWriter writer = router.AddLogWriter(tempFolder);
                Verify.IsNotNull(nameof(writer), writer);
                logFileName = writer.LogFileName;

                TrackEventSource source = new TrackEventSource(router, "test");

                Enumerable.Range(0, 100).Run(x =>
                {
                    source.Verbose(context, $"{x}_verbose message");
                    source.Info(context, $"{x}_info message");
                    source.Critical(context, $"{x}_critical message");
                });
            }

            var readList = new List <EventData>();

            using (var reader = new LogFileReader(logFileName).Open())
            {
                IEnumerable <EventData> readItems = reader.Read(500);
                readItems.Should().NotBeNull();
                readItems.Any().Should().BeTrue();

                readList.AddRange(readItems);
            }

            readList.Count.Should().Be(300);

            IEnumerator <EventData> dataEnumerable = readList.GetEnumerator();

            Enumerable.Range(0, 100).Run(x =>
            {
                EventData ev;

                dataEnumerable.MoveNext().Should().BeTrue();
                ev = dataEnumerable.Current;
                ev.EventSourceName.Should().Be("test");
                ev.EventName.Should().Be("Verbose");
                ev.TelemetryLevel.Should().Be(TelemetryLevel.Verbose);
                ev.Cv.Should().Be(context.Cv.ToString());
                ev.Tag.Should().Be(context.Tag.ToString());
                ev.Message.Should().Be($"{x}_verbose message");

                dataEnumerable.MoveNext().Should().BeTrue();
                ev = dataEnumerable.Current;
                ev.EventSourceName.Should().Be("test");
                ev.EventName.Should().Be("Info");
                ev.TelemetryLevel.Should().Be(TelemetryLevel.Informational);
                ev.Cv.Should().Be(context.Cv.ToString());
                ev.Tag.Should().Be(context.Tag.ToString());
                ev.Message.Should().Be($"{x}_info message");

                dataEnumerable.MoveNext().Should().BeTrue();
                ev = dataEnumerable.Current;
                ev.EventSourceName.Should().Be("test");
                ev.EventName.Should().Be("Critical");
                ev.TelemetryLevel.Should().Be(TelemetryLevel.Critical);
                ev.Cv.Should().Be(context.Cv.ToString());
                ev.Tag.Should().Be(context.Tag.ToString());
                ev.Message.Should().Be($"{x}_critical message");
            });
        }
Exemplo n.º 2
0
        public void MultipleTrackEventLogFileTest()
        {
            IWorkContext context    = WorkContext.Empty;
            string       tempFolder = Path.Combine(Path.GetTempPath(), "TelemetryTest");

            Directory.CreateDirectory(tempFolder);
            string logFileName;

            using (var logWriter = new LogFileWriter(tempFolder).Open())
            {
                logFileName = logWriter.LogFileName;

                EventRouter router = new EventRouter(context)
                                     .Register("test", (x, _) => logWriter.Write(x));

                TrackEventSource source = new TrackEventSource(router, "test");

                Enumerable.Range(0, 100).Run(x =>
                {
                    source.Verbose(context, $"{x}_verbose message");
                    source.Info(context, $"{x}_info message");
                    source.Critical(context, $"{x}_critical message");
                });

                Thread.Sleep(TimeSpan.FromSeconds(1));
            }

            var readList = new List <EventData>();

            using (var reader = new LogFileReader(logFileName).Open())
            {
                while (true)
                {
                    IEnumerable <EventData> readItems = reader.Read(500);
                    readItems.Should().NotBeNull();
                    if (!readItems.Any())
                    {
                        break;
                    }

                    readList.AddRange(readItems);
                }
            }

            readList.Count.Should().Be(300);

            IEnumerator <EventData> dataEnumerable = readList.GetEnumerator();

            Enumerable.Range(0, 100).Run(x =>
            {
                EventData ev;

                dataEnumerable.MoveNext().Should().BeTrue();
                ev = dataEnumerable.Current;
                ev.EventSourceName.Should().Be("test");
                ev.EventName.Should().Be("Verbose");
                ev.TelemetryLevel.Should().Be(TelemetryLevel.Verbose);
                ev.Cv.Should().Be(context.Cv.ToString());
                ev.Tag.Should().Be(context.Tag.ToString());
                ev.Message.Should().Be($"{x}_verbose message");

                dataEnumerable.MoveNext().Should().BeTrue();
                ev = dataEnumerable.Current;
                ev.EventSourceName.Should().Be("test");
                ev.EventName.Should().Be("Info");
                ev.TelemetryLevel.Should().Be(TelemetryLevel.Informational);
                ev.Cv.Should().Be(context.Cv.ToString());
                ev.Tag.Should().Be(context.Tag.ToString());
                ev.Message.Should().Be($"{x}_info message");

                dataEnumerable.MoveNext().Should().BeTrue();
                ev = dataEnumerable.Current;
                ev.EventSourceName.Should().Be("test");
                ev.EventName.Should().Be("Critical");
                ev.TelemetryLevel.Should().Be(TelemetryLevel.Critical);
                ev.Cv.Should().Be(context.Cv.ToString());
                ev.Tag.Should().Be(context.Tag.ToString());
                ev.Message.Should().Be($"{x}_critical message");
            });
        }