From e5d3f0cf4c4d7c6cd06755ee7e34f24d764b7595 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Thu, 4 Jan 2018 18:54:39 -0700 Subject: [PATCH 1/3] Improve error logging for exec of pscommands --- .../Session/PowerShellContext.cs | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index 6416d1129..0c2e48f86 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -511,13 +511,43 @@ await Task.Factory.StartNew>( if (this.powerShell.HadErrors) { - string errorMessage = "Execution completed with errors:\r\n\r\n"; + // Get the command/params that we were trying execute as a string in order to log it + string commandText = ""; + foreach (var cmd in psCommand.Commands) + { + commandText += cmd.CommandText; + foreach (var param in cmd.Parameters) + { + commandText += $" -{param.Name} {param.Value}"; + } + commandText += ";"; + } + + var strBld = new StringBuilder(1024); + strBld.Append($"Execution of command '{commandText}' completed with errors:\r\n\r\n"); + int i = 1; foreach (var error in this.powerShell.Streams.Error) { - errorMessage += error.ToString() + "\r\n"; + if (i > 1) strBld.Append("\r\n\r\n"); + strBld.Append($"Error #{i++}:\r\n"); + strBld.Append(error.ToString() + "\r\n"); + strBld.Append("ScriptStackTrace:\r\n"); + strBld.Append(error.ScriptStackTrace + "\r\n"); + strBld.Append($"Exception {error.Exception.ToString()}"); + Exception innerEx = error.Exception?.InnerException; + while (innerEx != null) + { + strBld.Append($"InnerException {innerEx.ToString()}"); + innerEx = innerEx.InnerException; + } } + // We've reported these errors, clear them so they don't keep showing up. + this.powerShell.Streams.Error.Clear(); + + var errorMessage = strBld.ToString(); + errorMessages?.Append(errorMessage); Logger.Write(LogLevel.Error, errorMessage); From 4ebdd5f0753f0ea33f52742231df155d9c1e08a4 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Fri, 5 Jan 2018 16:41:58 -0700 Subject: [PATCH 2/3] Handle null ScriptStackTrace - not sure it's possible but just in case --- src/PowerShellEditorServices/Session/PowerShellContext.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index 0c2e48f86..fc59a9616 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -533,8 +533,8 @@ await Task.Factory.StartNew>( strBld.Append($"Error #{i++}:\r\n"); strBld.Append(error.ToString() + "\r\n"); strBld.Append("ScriptStackTrace:\r\n"); - strBld.Append(error.ScriptStackTrace + "\r\n"); - strBld.Append($"Exception {error.Exception.ToString()}"); + strBld.Append((error.ScriptStackTrace ?? "") + "\r\n"); + strBld.Append($"Exception {error.Exception?.ToString() ?? ""}"); Exception innerEx = error.Exception?.InnerException; while (innerEx != null) { From e39672f3ed7f532a4aafaa2d1389a785939c94d9 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Fri, 5 Jan 2018 16:59:03 -0700 Subject: [PATCH 3/3] Tweak log output format and kick new build --- src/PowerShellEditorServices/Session/PowerShellContext.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index fc59a9616..1aba0daef 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -534,11 +534,11 @@ await Task.Factory.StartNew>( strBld.Append(error.ToString() + "\r\n"); strBld.Append("ScriptStackTrace:\r\n"); strBld.Append((error.ScriptStackTrace ?? "") + "\r\n"); - strBld.Append($"Exception {error.Exception?.ToString() ?? ""}"); + strBld.Append($"Exception:\r\n {error.Exception?.ToString() ?? ""}"); Exception innerEx = error.Exception?.InnerException; while (innerEx != null) { - strBld.Append($"InnerException {innerEx.ToString()}"); + strBld.Append($"InnerException:\r\n {innerEx.ToString()}"); innerEx = innerEx.InnerException; } }