Skip to content

Commit 9b5f8b9

Browse files
authored
[Config] add config to disable write error to file system (#403)
* disable error records persistence * split RecordDebugMessage and FlushDebugMessages * invoke RecordDebugMessages only when write error and throw terminating error * combine if condition in a bool function * Uppercase first letter for method name
1 parent 7984785 commit 9b5f8b9

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/Authentication.Abstractions/Models/ConfigKeysForCommon.cs

+3
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@ public static class ConfigKeysForCommon
2929
public const string EnableDataCollection = "EnableDataCollection";
3030
public const string EnableTestCoverage = "EnableTestCoverage";
3131
public const string CheckForUpgrade = "CheckForUpgrade";
32+
//Use DisableErrorRecordsPersistence as opt-out for now, will replace it with EnableErrorRecordsPersistence as opt-in at next major release (November 2023)
33+
public const string DisableErrorRecordsPersistence = "DisableErrorRecordsPersistence";
34+
public const string EnableErrorRecordsPersistence = "EnableErrorRecordsPersistence";
3235
}
3336
}

src/Common/AzurePSCmdlet.cs

+19-8
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,11 @@ protected void WriteSurvey()
499499
}
500500
protected new void WriteError(ErrorRecord errorRecord)
501501
{
502-
FlushDebugMessages(IsDataCollectionAllowed());
502+
FlushDebugMessages();
503+
if (ShouldRecordDebugMessages())
504+
{
505+
RecordDebugMessages();
506+
}
503507
if (_qosEvent != null && errorRecord != null)
504508
{
505509
_qosEvent.Exception = errorRecord.Exception;
@@ -515,7 +519,11 @@ protected void WriteSurvey()
515519

516520
protected new void ThrowTerminatingError(ErrorRecord errorRecord)
517521
{
518-
FlushDebugMessages(IsDataCollectionAllowed());
522+
FlushDebugMessages();
523+
if (ShouldRecordDebugMessages())
524+
{
525+
RecordDebugMessages();
526+
}
519527
base.ThrowTerminatingError(errorRecord);
520528
}
521529

@@ -634,13 +642,8 @@ protected void SafeWriteOutputPSObject(string typeName, params object[] args)
634642
WriteObject(customObject);
635643
}
636644

637-
private void FlushDebugMessages(bool record = false)
645+
private void FlushDebugMessages()
638646
{
639-
if (record)
640-
{
641-
RecordDebugMessages();
642-
}
643-
644647
string message;
645648
while (DebugMessages.TryDequeue(out message))
646649
{
@@ -777,6 +780,14 @@ private void RecordDebugMessages()
777780
}
778781
}
779782

783+
//Use DisableErrorRecordsPersistence as opt-out for now, will replace it with EnableErrorRecordsPersistence as opt-in at next major release (November 2023)
784+
private bool ShouldRecordDebugMessages()
785+
{
786+
return (!AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var configManager)
787+
|| !configManager.GetConfigValue<bool>(ConfigKeysForCommon.DisableErrorRecordsPersistence))
788+
&& IsDataCollectionAllowed();
789+
}
790+
780791
/// <summary>
781792
/// Invoke this method when the cmdlet is completed or terminated.
782793
/// </summary>

0 commit comments

Comments
 (0)