Esempio n. 1
0
 public void Register(AsyncCmdlet cmdlet)
 {
     if (cmdlet == null)
     {
         throw new ArgumentNullException(nameof(cmdlet));
     }
     _cmdlets[cmdlet.GetHashCode()] = cmdlet;
     //System.Console.WriteLine($"Register: {cmdlet.GetHashCode()}");
 }
Esempio n. 2
0
        private void EmitToCmdLet(AsyncCmdlet cmdlet, LogEvent logEvent)
        {
            using var strWriter = new StringWriter();
            TextFormatter.Format(logEvent, strWriter);
            try {
                var msg = $"{strWriter}";
                switch (logEvent.Level)
                {
                // -Verbose
                case LogEventLevel.Verbose:
                case LogEventLevel.Information:
                    cmdlet.WriteVerbose(msg);
                    break;

                // -Debug
                case LogEventLevel.Debug:
                    cmdlet.WriteDebug(msg);
                    break;

                // The Write-Warning cmdlet writes a warning message to the PowerShell host
                // The response to the warning depends on the value of the user's $WarningPreference variable and the use of the WarningAction common parameter.
                case LogEventLevel.Warning:
                    cmdlet.WriteWarning(msg);
                    break;

                // The Write-Error cmdlet declares a non-terminating error.
                case LogEventLevel.Error:
                    //_cmdlet.WriteDebug("error: " + strWriter.ToString());
                    var ex = logEvent.Exception;
                    if (logEvent.Exception == null)
                    {
                        ex = new Exception();
                    }

                    var er = new ErrorRecord(ex, errorId: msg, errorCategory: ErrorCategory.NotSpecified, targetObject: null);
                    cmdlet.WriteError(er);
                    break;

                case LogEventLevel.Fatal:
                    //_cmdlet.WriteDebug("fatal: " + strWriter.ToString());
                    var fatal = new ErrorRecord(logEvent.Exception, errorId: msg, errorCategory: ErrorCategory.InvalidOperation, targetObject: null);
                    cmdlet.ThrowTerminatingError(fatal);
                    break;

                default:
                    cmdlet.WriteDebug(msg);
                    break;
                }
            }
            catch (PipelineStoppedException pse) {
                Debug.WriteLine(pse.Message);
            }
            catch (PSInvalidOperationException psiop) {
                Debug.WriteLine(psiop.Message);
            }
        }