diff --git a/src/Authentication.Abstractions/Models/ConfigKeysForCommon.cs b/src/Authentication.Abstractions/Models/ConfigKeysForCommon.cs index 47065ad4b3..ce69f5fbc6 100644 --- a/src/Authentication.Abstractions/Models/ConfigKeysForCommon.cs +++ b/src/Authentication.Abstractions/Models/ConfigKeysForCommon.cs @@ -29,5 +29,8 @@ public static class ConfigKeysForCommon public const string EnableDataCollection = "EnableDataCollection"; public const string EnableTestCoverage = "EnableTestCoverage"; public const string CheckForUpgrade = "CheckForUpgrade"; + //Use DisableErrorRecordsPersistence as opt-out for now, will replace it with EnableErrorRecordsPersistence as opt-in at next major release (November 2023) + public const string DisableErrorRecordsPersistence = "DisableErrorRecordsPersistence"; + public const string EnableErrorRecordsPersistence = "EnableErrorRecordsPersistence"; } } diff --git a/src/Common/AzurePSCmdlet.cs b/src/Common/AzurePSCmdlet.cs index 7a9a4d443e..c3e3c577ab 100644 --- a/src/Common/AzurePSCmdlet.cs +++ b/src/Common/AzurePSCmdlet.cs @@ -499,7 +499,11 @@ protected void WriteSurvey() } protected new void WriteError(ErrorRecord errorRecord) { - FlushDebugMessages(IsDataCollectionAllowed()); + FlushDebugMessages(); + if (ShouldRecordDebugMessages()) + { + RecordDebugMessages(); + } if (_qosEvent != null && errorRecord != null) { _qosEvent.Exception = errorRecord.Exception; @@ -515,7 +519,11 @@ protected void WriteSurvey() protected new void ThrowTerminatingError(ErrorRecord errorRecord) { - FlushDebugMessages(IsDataCollectionAllowed()); + FlushDebugMessages(); + if (ShouldRecordDebugMessages()) + { + RecordDebugMessages(); + } base.ThrowTerminatingError(errorRecord); } @@ -634,13 +642,8 @@ protected void SafeWriteOutputPSObject(string typeName, params object[] args) WriteObject(customObject); } - private void FlushDebugMessages(bool record = false) + private void FlushDebugMessages() { - if (record) - { - RecordDebugMessages(); - } - string message; while (DebugMessages.TryDequeue(out message)) { @@ -777,6 +780,14 @@ private void RecordDebugMessages() } } + //Use DisableErrorRecordsPersistence as opt-out for now, will replace it with EnableErrorRecordsPersistence as opt-in at next major release (November 2023) + private bool ShouldRecordDebugMessages() + { + return (!AzureSession.Instance.TryGetComponent(nameof(IConfigManager), out var configManager) + || !configManager.GetConfigValue(ConfigKeysForCommon.DisableErrorRecordsPersistence)) + && IsDataCollectionAllowed(); + } + /// /// Invoke this method when the cmdlet is completed or terminated. ///