Skip to content
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
50 changes: 21 additions & 29 deletions src/System.Management.Automation/engine/pipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,44 +220,33 @@ private void Log(string logElement, InvocationInfo invocation, PipelineExecution
}
}

if (!string.IsNullOrEmpty(logElement))
if (_needToLog && !string.IsNullOrEmpty(logElement))
Comment thread
daxian-dbw marked this conversation as resolved.
{
_eventLogBuffer ??= new List<string>();
_eventLogBuffer.Add(logElement);
Comment thread
daxian-dbw marked this conversation as resolved.
}
}

internal void LogToEventLog()
private void LogToEventLog()
{
if (NeedToLog())
// We check to see if there is anything in the buffer before we flush it.
// Flushing the empty buffer causes a measurable performance degradation.
if (_commands?.Count > 0 && _eventLogBuffer?.Count > 0)
{
// We check to see if the command is needs writing (or if there is anything in the buffer)
// before we flush it. Flushing the empty buffer causes a measurable performance degradation.
if (_commands == null || _commands.Count == 0 || _eventLogBuffer.Count == 0)
return;

MshLog.LogPipelineExecutionDetailEvent(_commands[0].Command.Context,
_eventLogBuffer,
_commands[0].Command.MyInvocation);
}
}

private bool NeedToLog()
{
if (_commands == null)
return false;

foreach (CommandProcessorBase commandProcessor in _commands)
{
MshCommandRuntime cmdRuntime = commandProcessor.Command.commandRuntime as MshCommandRuntime;

if (cmdRuntime != null && cmdRuntime.LogPipelineExecutionDetail)
return true;
InternalCommand firstCmd = _commands[0].Command;
MshLog.LogPipelineExecutionDetailEvent(
firstCmd.Context,
_eventLogBuffer,
firstCmd.MyInvocation);
}

return false;
// Clear the log buffer after writing the event.
_eventLogBuffer?.Clear();
}

private List<string> _eventLogBuffer = new List<string>();
private bool _needToLog = false;
private List<string> _eventLogBuffer;

#endregion

#region public_methods
Expand All @@ -273,7 +262,7 @@ private bool NeedToLog()
internal int Add(CommandProcessorBase commandProcessor)
{
commandProcessor.CommandRuntime.PipelineProcessor = this;
return AddCommand(commandProcessor, _commands.Count, false);
return AddCommand(commandProcessor, _commands.Count, readErrorQueue: false);
}

internal void AddRedirectionPipe(PipelineProcessor pipelineProcessor)
Expand Down Expand Up @@ -306,7 +295,7 @@ internal void AddRedirectionPipe(PipelineProcessor pipelineProcessor)
/// PipeAlreadyTaken: the downstream pipe of command <paramref name="readFromCommand"/>
/// is already taken
/// </exception>
internal int AddCommand(CommandProcessorBase commandProcessor, int readFromCommand, bool readErrorQueue)
private int AddCommand(CommandProcessorBase commandProcessor, int readFromCommand, bool readErrorQueue)
{
if (commandProcessor == null)
{
Expand Down Expand Up @@ -412,6 +401,9 @@ internal int AddCommand(CommandProcessorBase commandProcessor, int readFromComma

_commands.Add(commandProcessor);

// We will log event(s) about the pipeline execution details if any command in the pipeline requests that.
_needToLog |= commandProcessor.CommandRuntime.LogPipelineExecutionDetail;
Comment thread
daxian-dbw marked this conversation as resolved.

// We give the Command a pointer back to the
// PipelineProcessor so that it can check whether the
// command has been stopped.
Expand Down