/// <summary> /// Log the PowerShell Streams from a PowerShell invocation /// </summary> /// <param name="powershell">The PowerShell instance to log</param> public static void LogPowerShellResults( this System.Management.Automation.PowerShell powershell, Collection <PSObject> output, XunitTracingInterceptor xunitLogger) { if (output != null) { LogPowerShellStream <PSObject>(xunitLogger, output, "OUTPUT"); } if (xunitLogger != null && powershell.Commands != null && powershell.Commands.Commands != null && powershell.Commands.Commands.Count > 0) { xunitLogger.Information("================== COMMANDS =======================\n"); foreach (Command command in powershell.Commands.Commands) { xunitLogger.Information(string.Format("{0}\n", command.CommandText)); } xunitLogger.Information("===================================================\n"); } LogPowerShellStream <DebugRecord>(xunitLogger, powershell.Streams.Debug, "DEBUG"); LogPowerShellStream <string>(xunitLogger, powershell.Streams.Error.Select(FormatErrorRecord).ToList(), "ERROR"); LogPowerShellStream <ProgressRecord>(xunitLogger, powershell.Streams.Progress, "PROGRESS"); LogPowerShellStream <VerboseRecord>(xunitLogger, powershell.Streams.Verbose, "VERBOSE"); LogPowerShellStream <WarningRecord>(xunitLogger, powershell.Streams.Warning, "WARNING"); }
/// <summary> /// Logs a PowerShell exception thrown from PowerShell.Invoke, parsing the inner /// PowerShell error record if available /// </summary> /// <param name="runtimeException">The exception to parse</param> public static void LogPowerShellException( this System.Management.Automation.PowerShell powershell, Exception runtimeException, XunitTracingInterceptor xunitLogger) { if (xunitLogger != null) { xunitLogger.Information(string.Format("Caught Exception: {0}", runtimeException)); xunitLogger.Information(string.Format("Message: {0}", runtimeException.Message)); } IContainsErrorRecord recordContainer = runtimeException as IContainsErrorRecord; if (recordContainer != null) { ErrorRecord record = recordContainer.ErrorRecord; if (xunitLogger != null) { xunitLogger.Information(FormatErrorRecord(record)); } } if (runtimeException.InnerException != null) { powershell.LogPowerShellException(runtimeException.InnerException, xunitLogger); } }
/// <summary> /// Logs a PowerShell exception thrown from PowerShell.Invoke, parsing the inner /// PowerShell error record if available /// </summary> /// <param name="runtimeException">The exception to parse</param> public static void LogPowerShellException( this System.Management.Automation.PowerShell powershell, Exception runtimeException, XunitTracingInterceptor xunitLogger) { if (xunitLogger != null) { xunitLogger.Information(string.Format("Caught Exception: {0}", runtimeException)); xunitLogger.Information(string.Format("Message: {0}", runtimeException.Message)); } IContainsErrorRecord recordContainer = runtimeException as IContainsErrorRecord; if (recordContainer != null) { ErrorRecord record = recordContainer.ErrorRecord; if (xunitLogger != null) { xunitLogger.Information(string.Format( "PowerShell Error Record: {0}\nException:{1}\nDetails:{2}\nScript Stack Trace: {3}\n: Target: {4}\n", record, record.Exception, record.ErrorDetails, record.ScriptStackTrace, record.TargetObject)); } } if (runtimeException.InnerException != null) { powershell.LogPowerShellException(runtimeException.InnerException, xunitLogger); } }
/// <summary> /// Methods for invoking PowerShell scripts. /// </summary> /// <param name="logger">logger.</param> /// <param name="scripts">scripts.</param> public void RunPsTest(XunitTracingInterceptor logger, params string[] scripts) { var sf = new StackTrace().GetFrame(1); var callingClassType = sf.GetMethod().ReflectedType?.ToString(); var mockName = sf.GetMethod().Name; logger.Information(string.Format("Test method entered: {0}.{1}", callingClassType, mockName)); this.RunPsTestWorkflow( logger, () => scripts, null, callingClassType, mockName); logger.Information(string.Format("Test method finished: {0}.{1}", callingClassType, mockName)); }
public void RunPsTest(XunitTracingInterceptor logger, string tenant, params string[] scripts) { var sf = new StackTrace().GetFrame(1); var callingClassType = sf.GetMethod().ReflectedType?.ToString(); var mockName = sf.GetMethod().Name; logger.Information(string.Format("Test method entered: {0}.{1}", callingClassType, mockName)); _helper.TracingInterceptor = logger; RunPsTestWorkflow( () => scripts, // no custom cleanup null, callingClassType, mockName, tenant); logger.Information(string.Format("Test method finished: {0}.{1}", callingClassType, mockName)); }
public void RunPsTest(XunitTracingInterceptor logger, params string[] scripts) { var callingClassType = TestUtilities.GetCallingClass(2); var mockName = TestUtilities.GetCurrentMethodName(2); logger.Information(string.Format("Test method entered: {0}.{1}", callingClassType, mockName)); helper.TracingInterceptor = logger; RunPsTestWorkflow( () => scripts, // no custom initializer null, // no custom cleanup null, callingClassType, mockName); logger.Information(string.Format("Test method finished: {0}.{1}", callingClassType, mockName)); }
public void RunPsTest(XunitTracingInterceptor logger, string tenant, params string[] scripts) { #if !NETSTANDARD var callingClassType = TestUtilities.GetCallingClass(2); var mockName = TestUtilities.GetCurrentMethodName(2); #else StackTrace st = new StackTrace(); StackFrame sf = st.GetFrame(1); var callingClassType = sf.GetMethod().ReflectedType.ToString(); var mockName = sf.GetMethod().Name; #endif logger.Information(string.Format("Test method entered: {0}.{1}", callingClassType, mockName)); helper.TracingInterceptor = logger; RunPsTestWorkflow( () => scripts, // no custom cleanup null, callingClassType, mockName, tenant); logger.Information(string.Format("Test method finished: {0}.{1}", callingClassType, mockName)); }
/// <summary> /// Log a single PowerShell stream, using the given name /// </summary> /// <typeparam name="T">The type of the internal data record (different for every stream)</typeparam> /// <param name="stream">The stream to log</param> /// <param name="name">The name of the stream to print in the log</param> private static void LogPowerShellStream <T>( XunitTracingInterceptor xunitLogger, ICollection <T> stream, string name) { if (xunitLogger != null && stream != null && stream.Count > 0) { xunitLogger.Information("---------------------------------------------------------------\n"); xunitLogger.Information(string.Format("{0} STREAM\n", name)); xunitLogger.Information("---------------------------------------------------------------\n"); foreach (T item in stream) { if (item != null) { xunitLogger.Information(string.Format("{0}\n", item.ToString())); } } xunitLogger.Information("---------------------------------------------------------------\n"); xunitLogger.Information(""); } }