diff --git a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs index de36b9d724..ab4e1067ac 100644 --- a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs +++ b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs @@ -6947,15 +6947,7 @@ namespace System.Management.Automation.Language // - Log method invocation to AMSI Notifications (can throw PSSecurityException) // - Invoke method string targetName = methodInfo.ReflectedType?.FullName ?? string.Empty; - expr = Expression.Block( - Expression.Call( - CachedReflectionInfo.MemberInvocationLoggingOps_LogMemberInvocation, - Expression.Constant(targetName), - Expression.Constant(name), - Expression.NewArrayInit( - typeof(object), - args.Select(static e => e.Expression.Cast(typeof(object))))), - expr); + MaybeAddMemberInvocationLogging(expr, targetName, name, args); // If we're calling SteppablePipeline.{Begin|Process|End}, we don't want // to wrap exceptions - this is very much a special case to help error @@ -7566,6 +7558,33 @@ namespace System.Management.Automation.Language } } +#nullable enable + private static Expression MaybeAddMemberInvocationLogging( + Expression expr, + string targetName, + string name, + DynamicMetaObject[] args) + { +#if UNIX && !DEBUG + // For efficiency this is a no-op on non-Windows platforms in release builds. + return expr; +#else + Expression[] invocationArgs = new Expression[args.Length]; + for (int i = 0; i < args.Length; i++) + { + invocationArgs[i] = args[i].Expression.Cast(typeof(object)); + } + return Expression.Block( + Expression.Call( + CachedReflectionInfo.MemberInvocationLoggingOps_LogMemberInvocation, + Expression.Constant(targetName), + Expression.Constant(name), + Expression.NewArrayInit(typeof(object), invocationArgs)), + expr); +#endif + } +#nullable disable + #endregion } diff --git a/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs b/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs index 2a37bfb507..ddc70fabd5 100644 --- a/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs +++ b/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs @@ -3694,10 +3694,6 @@ namespace System.Management.Automation internal static void LogMemberInvocation(string targetName, string name, object[] args) { -#if UNIX && !DEBUG - // For efficiency this is a no-op on non-Windows platforms in release builds. - return; -#else try { var contentName = "PowerShellMemberInvocation"; @@ -3745,7 +3741,6 @@ namespace System.Management.Automation Console.WriteLine($"!!! Amsi notification report exception: {ex} !!!"); } } -#endif } } }