public async Task <int> Collect(CancellationToken ct, List <string> counter_list, string counters, IConsole console, int processId, int refreshInterval, CountersExportFormat format, string output, string name, string diagnosticPort, bool resumeRuntime) { if (!ProcessLauncher.Launcher.HasChildProc && !CommandUtils.ValidateArgumentsForAttach(processId, name, diagnosticPort, out _processId)) { return(0); } shouldExit = new ManualResetEvent(false); _ct.Register(() => shouldExit.Set()); DiagnosticsClientBuilder builder = new DiagnosticsClientBuilder("dotnet-counters", 10); using (DiagnosticsClientHolder holder = await builder.Build(ct, _processId, diagnosticPort, showChildIO: false, printLaunchCommand: false)) { if (holder == null) { return(1); } try { InitializeCounterList(counters, counter_list); _ct = ct; _console = console; _interval = refreshInterval; _output = output; _diagnosticsClient = holder.Client; if (_output.Length == 0) { _console.Error.WriteLine("Output cannot be an empty string"); return(0); } if (format == CountersExportFormat.csv) { _renderer = new CSVExporter(output); } else if (format == CountersExportFormat.json) { // Try getting the process name. string processName = ""; try { if (ProcessLauncher.Launcher.HasChildProc) { _processId = ProcessLauncher.Launcher.ChildProc.Id; } processName = Process.GetProcessById(_processId).ProcessName; } catch (Exception) { } _renderer = new JSONExporter(output, processName); } else { _console.Error.WriteLine($"The output format {format} is not a valid output format."); return(0); } shouldResumeRuntime = ProcessLauncher.Launcher.HasChildProc || !string.IsNullOrEmpty(diagnosticPort) || resumeRuntime; int ret = await Start(); return(ret); } catch (OperationCanceledException) { try { _session.Stop(); } catch (Exception) { } // session.Stop() can throw if target application already stopped before we send the stop command. return(1); } } }
public async Task <int> Collect(CancellationToken ct, List <string> counter_list, IConsole console, int processId, int refreshInterval, CountersExportFormat format, string output, string name) { if (name != null) { if (processId != 0) { Console.WriteLine("Can only specify either --name or --process-id option."); return(0); } processId = CommandUtils.FindProcessIdWithName(name); if (processId < 0) { return(0); } } try { _ct = ct; _counterList = counter_list; // NOTE: This variable name has an underscore because that's the "name" that the CLI displays. System.CommandLine doesn't like it if we change the variable to camelcase. _console = console; _processId = processId; _interval = refreshInterval; _output = output; _diagnosticsClient = new DiagnosticsClient(processId); if (_output.Length == 0) { _console.Error.WriteLine("Output cannot be an empty string"); return(0); } if (format == CountersExportFormat.csv) { _renderer = new CSVExporter(output); } else if (format == CountersExportFormat.json) { // Try getting the process name. string processName = ""; try { processName = Process.GetProcessById(_processId).ProcessName; } catch (Exception) { } _renderer = new JSONExporter(output, processName);; } else { _console.Error.WriteLine($"The output format {format} is not a valid output format."); return(0); } return(await Start()); } catch (OperationCanceledException) { } return(1); }
public async Task <int> Collect(CancellationToken ct, List <string> counter_list, string counters, IConsole console, int processId, int refreshInterval, CountersExportFormat format, string output, string name) { int ret = 1; if (!ValidateAndSetProcessId(processId, name)) { return(0); } try { InitializeCounterList(counters, counter_list); _ct = ct; _console = console; _interval = refreshInterval; _output = output; if (_output.Length == 0) { _console.Error.WriteLine("Output cannot be an empty string"); return(0); } if (!BuildDiagnosticsClient()) { ProcessLauncher.Launcher.Cleanup(); return(0); } if (format == CountersExportFormat.csv) { _renderer = new CSVExporter(output); } else if (format == CountersExportFormat.json) { // Try getting the process name. string processName = ""; try { if (ProcessLauncher.Launcher.HasChildProc) { _processId = ProcessLauncher.Launcher.ChildProc.Id; } processName = Process.GetProcessById(_processId).ProcessName; } catch (Exception) { } _renderer = new JSONExporter(output, processName); } else { _console.Error.WriteLine($"The output format {format} is not a valid output format."); return(0); } ret = await Start(); } catch (OperationCanceledException) { } finally { ProcessLauncher.Launcher.Cleanup(); } return(ret); }
public async Task <int> Collect( CancellationToken ct, List <string> counter_list, string counters, IConsole console, int processId, int refreshInterval, CountersExportFormat format, string output, string name, string diagnosticPort, bool resumeRuntime, int maxHistograms, int maxTimeSeries) { try { // System.CommandLine does have an option to specify arguments as uint and it would validate they are non-negative. However the error // message is "Cannot parse argument '-1' for option '--maxTimeSeries' as expected type System.UInt32" which is not as user friendly. // If there was another option to leverage System.CommandLine that provides a little more user friendly error message we could switch // to it. ValidateNonNegative(maxHistograms, nameof(maxHistograms)); ValidateNonNegative(maxTimeSeries, nameof(maxTimeSeries)); if (!ProcessLauncher.Launcher.HasChildProc && !CommandUtils.ValidateArgumentsForAttach(processId, name, diagnosticPort, out _processId)) { return(ReturnCode.ArgumentError); } _ct.Register(() => _shouldExit.TrySetResult(ReturnCode.Ok)); DiagnosticsClientBuilder builder = new DiagnosticsClientBuilder("dotnet-counters", 10); using (DiagnosticsClientHolder holder = await builder.Build(ct, _processId, diagnosticPort, showChildIO: false, printLaunchCommand: false)) { if (holder == null) { return(ReturnCode.Ok); } try { _console = console; // the launch command may misinterpret app arguments as the old space separated // provider list so we need to ignore it in that case _counterList = ConfigureCounters(counters, _processId != 0 ? counter_list : null); _ct = ct; _interval = refreshInterval; _maxHistograms = maxHistograms; _maxTimeSeries = maxTimeSeries; _output = output; _diagnosticsClient = holder.Client; if (_output.Length == 0) { _console.Error.WriteLine("Output cannot be an empty string"); return(ReturnCode.ArgumentError); } if (format == CountersExportFormat.csv) { _renderer = new CSVExporter(output); } else if (format == CountersExportFormat.json) { // Try getting the process name. string processName = ""; try { if (ProcessLauncher.Launcher.HasChildProc) { _processId = ProcessLauncher.Launcher.ChildProc.Id; } processName = Process.GetProcessById(_processId).ProcessName; } catch (Exception) { } _renderer = new JSONExporter(output, processName); } else { _console.Error.WriteLine($"The output format {format} is not a valid output format."); return(ReturnCode.ArgumentError); } _resumeRuntime = resumeRuntime; int ret = await Start(); return(ret); } catch (OperationCanceledException) { try { _session.Stop(); } catch (Exception) { } // session.Stop() can throw if target application already stopped before we send the stop command. return(ReturnCode.Ok); } } } catch (CommandLineErrorException e) { console.Error.WriteLine(e.Message); return(ReturnCode.ArgumentError); } }