From 308e51519c61a2f1cfe40bd54d10177fe2054efa Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch> Date: Tue, 23 Apr 2019 21:34:44 +0200 Subject: [PATCH] Generalize process names logging to SystemChecks.LogRunningProcesses() Relates to #64, #52 --- ATxCommon/SystemChecks.cs | 34 ++++++++++++++++++++++++++++++++ ATxDiagnostics/ATxDiagnostics.cs | 10 +--------- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/ATxCommon/SystemChecks.cs b/ATxCommon/SystemChecks.cs index e0ce3a3..7f8231b 100644 --- a/ATxCommon/SystemChecks.cs +++ b/ATxCommon/SystemChecks.cs @@ -115,5 +115,39 @@ namespace ATxCommon return username == ""; } + + /// <summary> + /// Log names of running processes if loglevel is set to debug. + /// </summary> + /// <param name="longFormat">By default only the process names will be printed to the + /// log, enclosed by square brackets (e.g. [explorer]). If "longFormat" is set to true, + /// each process name will be printed on a separate line, followed by the title of the + /// corresponding main window (if existing).</param> + public static void LogRunningProcesses(bool longFormat=false) { + if (!Log.IsDebugEnabled) + return; + + if (longFormat) + Log.Debug("\n\n>>>>>>>>>>>> running processes >>>>>>>>>>>>"); + + var procs = ""; + foreach (var running in Process.GetProcesses()) { + if (longFormat) { + var title = running.MainWindowTitle; + if (title.Length > 0) { + title = " (\"" + title + "\")"; + } + Log.Debug(" - {0}{1}", running.ProcessName, title); + } else { + procs += $", [{running.ProcessName}]"; + } + } + + if (longFormat) { + Log.Debug("\n<<<<<<<<<<<< running processes <<<<<<<<<<<<\n"); + } else { + Log.Debug("Currently running processes: {0}", procs.Substring(2)); + } + } } } diff --git a/ATxDiagnostics/ATxDiagnostics.cs b/ATxDiagnostics/ATxDiagnostics.cs index 31de4c4..e47c2fc 100644 --- a/ATxDiagnostics/ATxDiagnostics.cs +++ b/ATxDiagnostics/ATxDiagnostics.cs @@ -41,15 +41,7 @@ namespace ATxDiagnostics Log.Debug("Free space on drive [C:]: " + Conv.BytesToString(SystemChecks.GetFreeDriveSpace("C:"))); - Log.Debug("\n\n>>>>>>>>>>>> running processes >>>>>>>>>>>>"); - foreach (var running in Process.GetProcesses()) { - var title = running.MainWindowTitle; - if (title.Length > 0) { - title = " (\"" + title + "\")"; - } - Log.Debug(" - {0}{1}", running.ProcessName, title); - } - Log.Debug("\n<<<<<<<<<<<< running processes <<<<<<<<<<<<\n"); + SystemChecks.LogRunningProcesses(true); if (perfMonitors.Contains("CPU")) { Log.Info("Watching CPU load using ATxCommon.Monitoring..."); -- GitLab