internal static void LogEngineLifecycleEvent( ExecutionContext executionContext, EngineState engineState, InvocationInfo invocationInfo) { using (MshLog._trace.TraceMethod()) { if (executionContext == null) { MshLog._trace.NewArgumentNullException(nameof(executionContext)); } else { if (!MshLog.NeedToLogEngineLifecycleEvent(executionContext)) { return; } EngineState engineState1 = MshLog.GetEngineState(executionContext); if (engineState == engineState1) { return; } MshLog.GetLogProvider(executionContext).LogEngineLifecycleEvent(MshLog.GetLogContext(executionContext, invocationInfo), engineState, engineState1); MshLog.SetEngineState(executionContext, engineState); } } }
private void InitCommon() { // set the metadata this.Command.CommandInfo = this.CommandInfo; // set the ObsoleteAttribute of the current command _obsoleteAttribute = this.CommandInfo.CommandMetadata.Obsolete; // set the execution context this.Command.Context = this._context; // Now set up the command runtime for this command. try { this.commandRuntime = new MshCommandRuntime(_context, this.CommandInfo, this.Command); this.Command.commandRuntime = this.commandRuntime; } catch (Exception e) // Catch-all OK, 3rd party callout. { // Log a command health event MshLog.LogCommandHealthEvent( this._context, e, Severity.Warning); throw; } }
internal void WriteError(ErrorRecord errorRecord) { if (this.Stopping) { PipelineStoppedException exception = new PipelineStoppedException(); throw exception; } if (this.streamErrors) { if (this.command == null) { throw PSTraceSource.NewInvalidOperationException("SessionStateStrings", "ErrorStreamingNotEnabled", new object[0]); } tracer.WriteLine("Writing error package to command error pipe", new object[0]); this.command.WriteError(errorRecord); } else { this.accumulatedErrorObjects.Add(errorRecord); if ((errorRecord.ErrorDetails != null) && (errorRecord.ErrorDetails.TextLookupError != null)) { Exception textLookupError = errorRecord.ErrorDetails.TextLookupError; errorRecord.ErrorDetails.TextLookupError = null; MshLog.LogProviderHealthEvent(this.ExecutionContext, this.ProviderInstance.ProviderInfo.Name, textLookupError, Severity.Warning); } } }
internal static void LogCommandLifecycleEvent( ExecutionContext executionContext, CommandState commandState, InvocationInfo invocationInfo) { using (MshLog._trace.TraceMethod()) { if (executionContext == null) { MshLog._trace.NewArgumentNullException(nameof(executionContext)); } else if (invocationInfo == null) { MshLog._trace.NewArgumentNullException(nameof(invocationInfo)); } else { if (!MshLog.NeedToLogCommandLifecycleEvent(executionContext)) { return; } MshLog.GetLogProvider(executionContext).LogCommandLifecycleEvent(MshLog.GetLogContext(executionContext, invocationInfo), commandState); } } }
internal static void LogProviderHealthEvent( ExecutionContext executionContext, string providerName, Exception exception, Severity severity) { using (MshLog._trace.TraceMethod()) { if (executionContext == null) { MshLog._trace.NewArgumentNullException(nameof(executionContext)); } else if (exception == null) { MshLog._trace.NewArgumentNullException(nameof(exception)); } else { if (!MshLog.NeedToLogProviderHealthEvent(executionContext)) { return; } InvocationInfo invocationInfo = (InvocationInfo)null; if (exception is IContainsErrorRecord containsErrorRecord && containsErrorRecord.ErrorRecord != null) { invocationInfo = containsErrorRecord.ErrorRecord.InvocationInfo; } MshLog.GetLogProvider(executionContext).LogProviderHealthEvent(MshLog.GetLogContext(executionContext, invocationInfo, severity), providerName, exception); } } }
internal static void LogPipelineExecutionDetailEvent( ExecutionContext executionContext, List <string> detail, string scriptName, string commandLine) { using (MshLog._trace.TraceMethod()) { if (executionContext == null) { MshLog._trace.NewArgumentNullException(nameof(executionContext)); } else { if (!MshLog.NeedToLogPipelineExecutionDetailEvent(executionContext)) { return; } LogContext logContext = MshLog.GetLogContext(executionContext, (InvocationInfo)null); logContext.CommandLine = commandLine; logContext.ScriptName = scriptName; MshLog.GetLogProvider(executionContext).LogPipelineExecutionDetailEvent(logContext, detail); } } }
/// <summary> /// Constructs a new instance of a ProviderInvocationException /// using the specified data /// </summary> /// /// <param name="resourceId"> /// The resource ID to use as the format message for the error. /// </param> /// /// <param name="resourceStr"> /// This is the message template string. /// </param> /// /// <param name="provider"> /// The provider information used when formatting the error message. /// </param> /// /// <param name="path"> /// The path used when formatting the error message. /// </param> /// /// <param name="e"> /// The exception that was thrown by the provider. This will be set as /// the ProviderInvocationException's InnerException and the message will /// be used when formatting the error message. /// </param> /// /// <param name="useInnerExceptionErrorMessage"> /// If true, the error record from the inner exception will be used if it contains one. /// If false, the error message specified by the resourceId will be used. /// </param> /// /// <returns> /// A new instance of a ProviderInvocationException. /// </returns> /// /// <exception cref="ProviderInvocationException"> /// Wraps <paramref name="e"/> in a ProviderInvocationException /// and then throws it. /// </exception> /// internal ProviderInvocationException NewProviderInvocationException( string resourceId, string resourceStr, ProviderInfo provider, string path, Exception e, bool useInnerExceptionErrorMessage) { // If the innerException was itself thrown by // ProviderBase.ThrowTerminatingError, it is already a // ProviderInvocationException, and we don't want to // re-wrap it. ProviderInvocationException pie = e as ProviderInvocationException; if (null != pie) { pie._providerInfo = provider; return(pie); } pie = new ProviderInvocationException(resourceId, resourceStr, provider, path, e, useInnerExceptionErrorMessage); // Log a provider health event MshLog.LogProviderHealthEvent( ExecutionContext, provider.Name, pie, Severity.Warning); return(pie); }
internal static void LogEngineLifecycleEvent( ExecutionContext executionContext, EngineState engineState) { using (MshLog._trace.TraceMethod()) MshLog.LogEngineLifecycleEvent(executionContext, engineState, (InvocationInfo)null); }
internal static void LogEngineHealthEvent( ExecutionContext executionContext, Exception exception, Severity severity) { using (MshLog._trace.TraceMethod()) MshLog.LogEngineHealthEvent(executionContext, 100, exception, severity, (Dictionary <string, string>)null); }
internal static void LogSettingsEvent( ExecutionContext executionContext, string variableName, string newValue, string previousValue) { using (MshLog._trace.TraceMethod()) MshLog.LogSettingsEvent(executionContext, variableName, newValue, previousValue, (InvocationInfo)null); }
internal static void LogEngineHealthEvent( ExecutionContext executionContext, int eventId, Exception exception, Severity severity, EngineState newEngineState) { using (MshLog._trace.TraceMethod()) MshLog.LogEngineHealthEvent(executionContext, eventId, exception, severity, (Dictionary <string, string>)null, newEngineState); }
internal static void LogEngineHealthEvent( ExecutionContext executionContext, int eventId, Exception exception, Severity severity, Dictionary <string, string> additionalInfo) { using (MshLog._trace.TraceMethod()) MshLog.LogEngineHealthEvent(executionContext, eventId, exception, severity, additionalInfo, EngineState.None); }
private static LogProvider GetLogProvider(string shellId) { using (MshLog._trace.TraceMethod()) { Hashtable hashtable = Hashtable.Synchronized(MshLog._logProviders); if (hashtable[(object)shellId] == null) { hashtable[(object)shellId] = (object)MshLog.CreateLogProvider(shellId); } return((LogProvider)hashtable[(object)shellId]); } }
private void Init(CmdletInfo cmdletInformation) { Cmdlet cmdlet = (Cmdlet)null; Exception exception = (Exception)null; string errorIdAndResourceId = "CmdletNotFoundException"; try { cmdlet = (Cmdlet)Activator.CreateInstance(cmdletInformation.ImplementingType); } catch (TargetInvocationException ex) { CommandProcessor.tracer.TraceException((Exception)ex); CmdletInvocationException invocationException = new CmdletInvocationException(ex.InnerException, (InvocationInfo)null); MshLog.LogCommandHealthEvent(this.context, (Exception)invocationException, Severity.Warning); throw invocationException; } catch (MemberAccessException ex) { exception = (Exception)ex; } catch (TypeLoadException ex) { exception = (Exception)ex; } catch (InvalidCastException ex) { exception = (Exception)ex; errorIdAndResourceId = "CmdletDoesNotDeriveFromCmdletType"; } catch (Exception ex) { CommandProcessorBase.CheckForSevereException(ex); CommandProcessor.tracer.TraceException(ex); MshLog.LogCommandHealthEvent(this.context, ex, Severity.Warning); throw; } if (exception != null) { CommandProcessor.tracer.TraceException(exception); MshLog.LogCommandHealthEvent(this.context, exception, Severity.Warning); CommandNotFoundException notFoundException = new CommandNotFoundException(cmdletInformation.Name, exception, errorIdAndResourceId, new object[1] { (object)exception.Message }); CommandProcessor.tracer.TraceException((Exception)notFoundException); throw notFoundException; } this.Command = (InternalCommand)cmdlet; this.InitCommon(); }
} // WriteObject /// <summary> /// Writes the error to the pipeline or accumulates the error in an internal /// buffer. /// </summary> /// <param name="errorRecord"> /// The error record to write to the pipeline or the internal buffer. /// </param> /// <exception cref="InvalidOperationException"> /// The CmdletProvider could not stream the error because no /// cmdlet was specified to stream the output through. /// </exception> /// <exception cref="PipelineStoppedException"> /// If the pipeline has been signaled for stopping but /// the provider calls this method. /// </exception> internal void WriteError(ErrorRecord errorRecord) { // Making sure to obey the StopProcessing by // throwing an exception anytime a provider tries // to WriteError if (Stopping) { PipelineStoppedException stopPipeline = new PipelineStoppedException(); throw stopPipeline; } if (_streamErrors) { if (_command != null) { s_tracer.WriteLine("Writing error package to command error pipe"); _command.WriteError(errorRecord); } else { InvalidOperationException e = PSTraceSource.NewInvalidOperationException( SessionStateStrings.ErrorStreamingNotEnabled); throw e; } } else { // Since we are not streaming, just add the object to the accumulatedErrorObjects _accumulatedErrorObjects.Add(errorRecord); if (errorRecord.ErrorDetails != null && errorRecord.ErrorDetails.TextLookupError != null) { Exception textLookupError = errorRecord.ErrorDetails.TextLookupError; errorRecord.ErrorDetails.TextLookupError = null; MshLog.LogProviderHealthEvent( this.ExecutionContext, this.ProviderInstance.ProviderInfo.Name, textLookupError, Severity.Warning); } } } // WriteError
private void InitCommon() { base.Command.CommandInfo = base.CommandInfo; base.Command.Context = base._context; try { base.commandRuntime = new MshCommandRuntime(base._context, base.CommandInfo, base.Command); base.Command.commandRuntime = base.commandRuntime; } catch (Exception exception) { CommandProcessorBase.CheckForSevereException(exception); MshLog.LogCommandHealthEvent(base._context, exception, Severity.Warning); throw; } }
private void InitCommon() { this.Command.CommandInfo = this.CommandInfo; this.Command.Context = this.context; try { this.commandRuntime = new MshCommandRuntime(this.context, this.CommandInfo, this.Command); this.Command.commandRuntime = (ICommandRuntime)this.commandRuntime; } catch (Exception ex) { CommandProcessorBase.CheckForSevereException(ex); CommandProcessor.tracer.TraceException(ex); MshLog.LogCommandHealthEvent(this.context, ex, Severity.Warning); throw; } }
internal static void CheckForSevereException(Exception e) { if ((e is AccessViolationException) || (e is StackOverflowException)) { try { if (!alreadyFailing) { alreadyFailing = true; MshLog.LogCommandHealthEvent(LocalPipeline.GetExecutionContextFromTLS(), e, Severity.Critical); } } finally { WindowsErrorReporting.FailFast(e); } } }
private void Init(CmdletInfo cmdletInformation) { Cmdlet cmdlet = null; Exception exception = null; string str = null; string cmdletDoesNotDeriveFromCmdletType = null; try { cmdlet = ConstructInstance(cmdletInformation.ImplementingType); if (cmdlet == null) { exception = new InvalidCastException(); str = "CmdletDoesNotDeriveFromCmdletType"; cmdletDoesNotDeriveFromCmdletType = DiscoveryExceptions.CmdletDoesNotDeriveFromCmdletType; } } catch (MemberAccessException exception2) { exception = exception2; } catch (TypeLoadException exception3) { exception = exception3; } catch (Exception exception4) { CommandProcessorBase.CheckForSevereException(exception4); CmdletInvocationException exception5 = new CmdletInvocationException(exception4, null); MshLog.LogCommandHealthEvent(base._context, exception5, Severity.Warning); throw exception5; } if (exception != null) { MshLog.LogCommandHealthEvent(base._context, exception, Severity.Warning); CommandNotFoundException exception6 = new CommandNotFoundException(cmdletInformation.Name, exception, str ?? "CmdletNotFoundException", cmdletDoesNotDeriveFromCmdletType ?? DiscoveryExceptions.CmdletNotFoundException, new object[] { exception.Message }); throw exception6; } base.Command = cmdlet; base.CommandScope = base.Context.EngineSessionState.CurrentScope; this.InitCommon(); }
internal static void LogPipelineExecutionDetailEvent( ExecutionContext executionContext, List <string> detail, InvocationInfo invocationInfo) { using (MshLog._trace.TraceMethod()) { if (executionContext == null) { MshLog._trace.NewArgumentNullException(nameof(executionContext)); } else { if (!MshLog.NeedToLogPipelineExecutionDetailEvent(executionContext)) { return; } MshLog.GetLogProvider(executionContext).LogPipelineExecutionDetailEvent(MshLog.GetLogContext(executionContext, invocationInfo), detail); } } }
internal static void LogProviderLifecycleEvent( ExecutionContext executionContext, string providerName, ProviderState providerState) { using (MshLog._trace.TraceMethod()) { if (executionContext == null) { MshLog._trace.NewArgumentNullException(nameof(executionContext)); } else { if (!MshLog.NeedToLogProviderLifecycleEvent(executionContext)) { return; } MshLog.GetLogProvider(executionContext).LogProviderLifecycleEvent(MshLog.GetLogContext(executionContext, (InvocationInfo)null), providerName, providerState); } } }
/// <summary> /// If there are any errors accumulated, the first error is thrown. /// </summary> /// <param name="wrapExceptionInProviderException"> /// If true, the error will be wrapped in a ProviderInvocationException before /// being thrown. If false, the error will be thrown as is. /// </param> /// <exception cref="ProviderInvocationException"> /// If <paramref name="wrapExceptionInProviderException"/> is true, the /// first exception that was written to the error pipeline by a CmdletProvider /// is wrapped and thrown. /// </exception> /// <exception> /// If <paramref name="wrapExceptionInProviderException"/> is false, /// the first exception that was written to the error pipeline by a CmdletProvider /// is thrown. /// </exception> internal void ThrowFirstErrorOrDoNothing(bool wrapExceptionInProviderException) { if (HasErrors()) { Collection <ErrorRecord> errors = GetAccumulatedErrorObjects(); if (errors != null && errors.Count > 0) { // Throw the first exception if (wrapExceptionInProviderException) { ProviderInfo providerInfo = null; if (this.ProviderInstance != null) { providerInfo = this.ProviderInstance.ProviderInfo; } ProviderInvocationException e = new ProviderInvocationException( providerInfo, errors[0]); // Log a provider health event MshLog.LogProviderHealthEvent( this.ExecutionContext, providerInfo != null ? providerInfo.Name : "unknown provider", e, Severity.Warning); throw e; } else { throw errors[0].Exception; } } } }
internal void ThrowFirstErrorOrDoNothing(bool wrapExceptionInProviderException) { if (this.HasErrors()) { Collection <ErrorRecord> accumulatedErrorObjects = this.GetAccumulatedErrorObjects(); if ((accumulatedErrorObjects != null) && (accumulatedErrorObjects.Count > 0)) { if (!wrapExceptionInProviderException) { throw accumulatedErrorObjects[0].Exception; } ProviderInfo provider = null; if (this.ProviderInstance != null) { provider = this.ProviderInstance.ProviderInfo; } ProviderInvocationException exception = new ProviderInvocationException(provider, accumulatedErrorObjects[0]); MshLog.LogProviderHealthEvent(this.ExecutionContext, (provider != null) ? provider.Name : "unknown provider", exception, Severity.Warning); throw exception; } } }
internal static void LogEngineHealthEvent( LogContext logContext, int eventId, Exception exception, Dictionary <string, string> additionalInfo) { using (MshLog._trace.TraceMethod()) { if (logContext == null) { MshLog._trace.NewArgumentNullException(nameof(logContext)); } else if (exception == null) { MshLog._trace.NewArgumentNullException(nameof(exception)); } else { MshLog.GetLogProvider(logContext).LogEngineHealthEvent(logContext, eventId, exception, additionalInfo); } } }
internal static void LogEngineHealthEvent( ExecutionContext executionContext, int eventId, Exception exception, Severity severity, Dictionary <string, string> additionalInfo, EngineState newEngineState) { using (MshLog._trace.TraceMethod()) { if (executionContext == null) { MshLog._trace.NewArgumentNullException(nameof(executionContext)); } else if (exception == null) { MshLog._trace.NewArgumentNullException(nameof(exception)); } else { if (!MshLog.NeedToLogEngineHealthEvent(executionContext)) { return; } InvocationInfo invocationInfo = (InvocationInfo)null; if (exception is IContainsErrorRecord containsErrorRecord && containsErrorRecord.ErrorRecord != null) { invocationInfo = containsErrorRecord.ErrorRecord.InvocationInfo; } MshLog.GetLogProvider(executionContext).LogEngineHealthEvent(MshLog.GetLogContext(executionContext, invocationInfo, severity), eventId, exception, additionalInfo); if (newEngineState == EngineState.None) { return; } MshLog.LogEngineLifecycleEvent(executionContext, newEngineState, invocationInfo); } } }
internal static void LogSettingsEvent( ExecutionContext executionContext, string variableName, string newValue, string previousValue, InvocationInfo invocationInfo) { using (MshLog._trace.TraceMethod()) { if (executionContext == null) { MshLog._trace.NewArgumentNullException(nameof(executionContext)); } else { if (!MshLog.NeedToLogSettingsEvent(executionContext)) { return; } MshLog.GetLogProvider(executionContext).LogSettingsEvent(MshLog.GetLogContext(executionContext, invocationInfo), variableName, newValue, previousValue); } } }
// Keep in sync: // S.M.A.CommandProcessorBase.CheckForSevereException // S.M.A.Internal.ConsoleHost.CheckForSevereException // S.M.A.Commands.CommandsCommon.CheckForSevereException // S.M.A.Commands.UtilityCommon.CheckForSevereException /// <summary> /// Checks whether the exception is a severe exception which should /// cause immediate process failure. /// </summary> /// <param name="e"></param> /// <remarks> /// CB says 02/23/2005: I personally would err on the side /// of treating OOM like an application exception, rather than /// a critical system failure.I think this will be easier to justify /// in Orcas, if we tease apart the two cases of OOM better. /// But even in Whidbey, how likely is it that we couldnt JIT /// some backout code? At that point, the process or possibly /// the machine is likely to stop executing soon no matter /// what you do in this routine. So I would just consider /// AccessViolationException. (I understand why you have SO here, /// at least temporarily). /// </remarks> internal static void CheckForSevereException(Exception e) { if (e is AccessViolationException || e is StackOverflowException) { try { if (!alreadyFailing) { alreadyFailing = true; // Get the ExecutionContext from the thread. ExecutionContext context = Runspaces.LocalPipeline.GetExecutionContextFromTLS(); // Log a command health event for this critical error. MshLog.LogCommandHealthEvent(context, e, Severity.Critical); } } finally { WindowsErrorReporting.FailFast(e); } } }
internal static void LogCommandLifecycleEvent( ExecutionContext executionContext, CommandState commandState, string commandName) { using (MshLog._trace.TraceMethod()) { if (executionContext == null) { MshLog._trace.NewArgumentNullException(nameof(executionContext)); } else { if (!MshLog.NeedToLogCommandLifecycleEvent(executionContext)) { return; } LogContext logContext = MshLog.GetLogContext(executionContext, (InvocationInfo)null); logContext.CommandName = commandName; MshLog.GetLogProvider(executionContext).LogCommandLifecycleEvent(logContext, commandState); } } }
internal static void ShouldRun( ExecutionContext context, PSHost host, CommandInfo commandInfo, CommandOrigin commandOrigin) { try { if (commandOrigin == CommandOrigin.Runspace && commandInfo.Visibility != SessionStateEntryVisibility.Public) { CommandNotFoundException notFoundException = new CommandNotFoundException(commandInfo.Name, (Exception)null, "CommandNotFoundException", new object[0]); CommandDiscovery.tracer.TraceException((Exception)notFoundException); throw notFoundException; } context.AuthorizationManager.ShouldRunInternal(commandInfo, commandOrigin, host); } catch (PSSecurityException ex) { CommandDiscovery.tracer.TraceException((Exception)ex); MshLog.LogCommandHealthEvent(context, (Exception)ex, Severity.Warning); MshLog.LogCommandLifecycleEvent(context, CommandState.Terminated, commandInfo.Name); throw; } }
/// <summary> /// Initializes the command's request object. /// </summary> /// <param name="cmdletInformation"> /// The information about the cmdlet. /// </param> /// <exception cref="CmdletInvocationException"> /// If the constructor for the cmdlet threw an exception. /// </exception> /// <exception cref="MemberAccessException"> /// The type referenced by <paramref name="cmdletInformation"/> refered to an /// abstract type or them member was invoked via a late-binding mechanism. /// </exception> /// <exception cref="TypeLoadException"> /// If <paramref name="cmdletInformation"/> refers to a type that is invalid. /// </exception> private void Init(CmdletInfo cmdletInformation) { Diagnostics.Assert(cmdletInformation != null, "Constructor should throw exception if LookupCommand returned null."); Cmdlet newCmdlet = null; Exception initError = null; string errorIdAndResourceId = null; string resourceStr = null; try { // Create the request object newCmdlet = ConstructInstance(cmdletInformation.ImplementingType); if (newCmdlet == null) { // We could test the inheritance before constructing, but that's // expensive. Much cheaper to just check for null. initError = new InvalidCastException(); errorIdAndResourceId = "CmdletDoesNotDeriveFromCmdletType"; resourceStr = DiscoveryExceptions.CmdletDoesNotDeriveFromCmdletType; } } catch (MemberAccessException memberAccessException) { initError = memberAccessException; } catch (TypeLoadException typeLoadException) { initError = typeLoadException; } catch (Exception e) // Catch-all OK, 3rd party callout. { // We don't have a Command or InvocationInfo at this point, // since the command failed to initialize. var commandException = new CmdletInvocationException(e, null); // Log a command health event MshLog.LogCommandHealthEvent( this._context, commandException, Severity.Warning); throw commandException; } if (initError != null) { // Log a command health event MshLog.LogCommandHealthEvent( this._context, initError, Severity.Warning); CommandNotFoundException exception = new CommandNotFoundException( cmdletInformation.Name, initError, errorIdAndResourceId ?? "CmdletNotFoundException", resourceStr ?? DiscoveryExceptions.CmdletNotFoundException, initError.Message); throw exception; } this.Command = newCmdlet; this.CommandScope = Context.EngineSessionState.CurrentScope; InitCommon(); }