Exemplo n.º 1
0
        public CombinedLogger(IEnumerable <ILogEvents> loggers, ErrorBehaviourOptions individualLoggerErrorBehaviour)
        {
            if (loggers == null)
            {
                throw new ArgumentNullException(nameof(loggers));
            }
            if ((individualLoggerErrorBehaviour != ErrorBehaviourOptions.Ignore) && (individualLoggerErrorBehaviour != ErrorBehaviourOptions.ThrowException))
            {
                // Note: Explicitly check for all valid values rather than using Enum.IsDefined since IsDefined uses reflection and logging should be as cheap as possible
                // (so reflection is best avoided)
                throw new ArgumentOutOfRangeException(nameof(individualLoggerErrorBehaviour));
            }

            Loggers = loggers.ToList().AsReadOnly();
            if (Loggers.Any(logger => logger == null))
            {
                throw new ArgumentException("Null reference encountered in loggers set");
            }
            IndividualLoggerErrorBehaviour = individualLoggerErrorBehaviour;
        }
Exemplo n.º 2
0
        public async Task <int> OnExecuteAsync(CommandLineApplication app, IConsole console)
        {
            var disconnectCts     = new CancellationTokenSource();
            var cancellationToken = console.GetCtrlCToken();

            if (!TryCreateClient(console, out var client))
            {
                return(1);
            }

            console.WriteLine("Connecting to application...");

            client.OnEventWritten += (evt) =>
            {
                // TODO: Format both kinds of messages ("Foo {0}" and "Foo {foo}")
                console.WriteLine($"{evt.ProviderName}/{evt.EventName}({evt.EventId}): {evt.Message}");
                for (var i = 0; i < evt.Payload.Count; i++)
                {
                    console.WriteLine($"  {evt.PayloadNames[i]}: {evt.Payload[i]}");
                }
            };

            client.OnEventCounterUpdated += (state) =>
            {
                console.WriteLine($"Counter: {state.ProviderName}/{state.CounterName} (Avg: {state.Mean}, StdDev: {state.StandardDeviation}, Count: {state.Count}, Min: {state.Min}, Max: {state.Max})");
            };

            client.Disconnected += (ex) =>
            {
                console.WriteLine("Disconnected");
                if (ex != null)
                {
                    console.Error.WriteLine(ex.ToString());
                }
                disconnectCts.Cancel();
            };

            await client.ConnectAsync();

            var enabledSomething = false;

            if (Loggers != null && Loggers.Any())
            {
                await client.EnableLoggersAsync(Loggers);

                enabledSomething = true;
            }

            if (Counters != null && Counters.Any())
            {
                await client.EnableCountersAsync(Counters);

                enabledSomething = true;
            }

            if (Providers != null && Providers.Any())
            {
                var requests = new List <EnableEventsRequest>(Providers.Count);
                foreach (var p in Providers)
                {
                    if (!TryCreateEventRequest(console, p, out var request))
                    {
                        return(1);
                    }
                    requests.Add(request);
                }
                await client.EnableEventsAsync(requests);

                enabledSomething = true;
            }

            if (!enabledSomething)
            {
                console.Error.WriteLine("At least one of '--provider', '--logger', or '--counter' must be provided.");
                return(1);
            }

            console.WriteLine("Connected, press Ctrl-C to terminate...");
            await CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, disconnectCts.Token).WaitForCancellationAsync();

            return(0);
        }