Beispiel #1
0
        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            return(Task.Run(async() =>
            {
                while (!stoppingToken.IsCancellationRequested)
                {
                    stoppingToken.ThrowIfCancellationRequested();

                    try
                    {
                        //TODO In multi-process scenarios, how do we decide which process to choose?
                        //One possibility is to enable metrics after a request to begin polling for metrics
                        IProcessInfo pi = await _services.GetProcessAsync(filter: null, stoppingToken);
                        await _pipeProcessor.Process(pi.Client, pi.Pid, Timeout.InfiniteTimeSpan, stoppingToken);
                    }
                    catch (Exception e) when(!(e is OperationCanceledException))
                    {
                        //Most likely we failed to resolve the pid. Attempt to do this again.
                        await Task.Delay(5000);
                    }
                }
            }, stoppingToken));
        }
Beispiel #2
0
        public async Task TestDiagnosticsEventPipeProcessorLogs()
        {
            var outputStream = new MemoryStream();

            await using (var testExecution = RemoteTestExecution.StartRemoteProcess("LoggerRemoteTest", _output))
            {
                //TestRunner should account for start delay to make sure that the diagnostic pipe is available.

                var loggerFactory = new LoggerFactory(new[] { new StreamingLoggerProvider(outputStream, LogFormat.Json) });

                DiagnosticsEventPipeProcessor diagnosticsEventPipeProcessor = new DiagnosticsEventPipeProcessor(
                    PipeMode.Logs,
                    loggerFactory,
                    Enumerable.Empty <IMetricsLogger>());

                var processingTask = diagnosticsEventPipeProcessor.Process(testExecution.TestRunner.Pid, TimeSpan.FromSeconds(10), CancellationToken.None);

                //Add a small delay to make sure diagnostic processor had a chance to initialize
                await Task.Delay(1000);

                //Send signal to proceed with event collection
                testExecution.Start();

                await processingTask;
                await diagnosticsEventPipeProcessor.DisposeAsync();

                loggerFactory.Dispose();
            }

            outputStream.Position = 0L;

            Assert.True(outputStream.Length > 0, "No data written by logging process.");

            using var reader = new StreamReader(outputStream);

            string firstMessage = reader.ReadLine();

            Assert.NotNull(firstMessage);

            LoggerTestResult result = JsonSerializer.Deserialize <LoggerTestResult>(firstMessage);

            Assert.Equal("Some warning message with 6", result.Message);
            Assert.Equal("LoggerRemoteTest", result.Category);
            Assert.Equal("Warning", result.LogLevel);
            Assert.Equal("0", result.EventId);
            Validate(result.Scopes, ("BoolValue", "true"), ("StringValue", "test"), ("IntValue", "5"));
            Validate(result.Arguments, ("arg", "6"));

            string secondMessage = reader.ReadLine();

            Assert.NotNull(secondMessage);

            result = JsonSerializer.Deserialize <LoggerTestResult>(secondMessage);
            Assert.Equal("Another message", result.Message);
            Assert.Equal("LoggerRemoteTest", result.Category);
            Assert.Equal("Warning", result.LogLevel);
            Assert.Equal("0", result.EventId);
            Assert.Equal(0, result.Scopes.Count);
            //We are expecting only the original format
            Assert.Equal(1, result.Arguments.Count);
        }
        public async Task TestDiagnosticsEventPipeProcessorLogs()
        {
            var outputStream = new MemoryStream();

            using (var testExecution = RemoteTest.StartRemoteProcess(LoggerRemoteTest.EntryPoint, nameof(LoggerRemoteTest), _output))
            {
                _output.WriteLine($"Started remote execution {testExecution.RemoteProcess.Process.ProcessName} {testExecution.RemoteProcess.Process.Id}");

                //Add a small delay to make sure the remote process had a chance to start and create the diagnostic pipe.
                await Task.Delay(1000);

                var loggerFactory = new LoggerFactory(new[] { new StreamingLoggerProvider(outputStream) });

                DiagnosticsEventPipeProcessor diagnosticsEventPipeProcessor = new DiagnosticsEventPipeProcessor(
                    ContextConfiguration,
                    PipeMode.Logs,
                    loggerFactory,
                    Enumerable.Empty <IMetricsLogger>());

                var processingTask = diagnosticsEventPipeProcessor.Process(testExecution.RemoteProcess.Process.Id, TimeSpan.FromSeconds(10), CancellationToken.None);

                //Add a small delay to make sure diagnostic processor had a chance to initialize
                await Task.Delay(1000);

                testExecution.Start();

                await processingTask;
                await diagnosticsEventPipeProcessor.DisposeAsync();

                loggerFactory.Dispose();
            }

            outputStream.Position = 0L;

            Assert.True(outputStream.Length > 0, "No data written by logging process.");

            using var reader = new StreamReader(outputStream);

            string firstMessage = reader.ReadLine();

            Assert.NotNull(firstMessage);

            LoggerTestResult result = JsonSerializer.Deserialize <LoggerTestResult>(firstMessage);

            Assert.Equal("Some warning message with 6", result.Message);
            Assert.Equal(nameof(LoggerRemoteTest), result.Category);
            Assert.Equal("Warning", result.LogLevel);
            Assert.Equal("0", result.EventId);
            Validate(result.Scopes, ("BoolValue", "true"), ("StringValue", "test"), ("IntValue", "5"));
            Validate(result.Arguments, ("arg", "6"));

            string secondMessage = reader.ReadLine();

            Assert.NotNull(secondMessage);

            result = JsonSerializer.Deserialize <LoggerTestResult>(secondMessage);
            Assert.Equal("Another message", result.Message);
            Assert.Equal(nameof(LoggerRemoteTest), result.Category);
            Assert.Equal("Warning", result.LogLevel);
            Assert.Equal("0", result.EventId);
            Assert.Equal(0, result.Scopes.Count);
            //We are expecting only the original format
            Assert.Equal(1, result.Arguments.Count);
        }