Skip to content

Improve error logging for exec of pscommands #598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 6, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/PowerShellEditorServices/Session/PowerShellContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,13 +511,43 @@ await Task.Factory.StartNew<IEnumerable<TResult>>(

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 ?? "<null>") + "\r\n");
strBld.Append($"Exception:\r\n {error.Exception?.ToString() ?? "<null>"}");
Exception innerEx = error.Exception?.InnerException;
while (innerEx != null)
{
strBld.Append($"InnerException:\r\n {innerEx.ToString()}");
innerEx = innerEx.InnerException;
}
}

// We've reported these errors, clear them so they don't keep showing up.
this.powerShell.Streams.Error.Clear();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, I could have sworn I added code for this in the past.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And there very well could be a better place to put this call. I contemplated clearing the error collection at the beginning of the method, before it runs the PSCommand. But I stuck with only clearing after reporting error message. BTW they weren't getting cleared. I kept seeing the same two import failures ($pseditor null) for every subsequent execution - profile loading, prompt eval, etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, this is the right place to do it. I just couldn't believe I hadn't actually added the code. Probably was in a branch somewhere that never got merged :/


var errorMessage = strBld.ToString();

errorMessages?.Append(errorMessage);
Logger.Write(LogLevel.Error, errorMessage);

Expand Down